问:
Spring Client Needs to Retrieve Connection from Pool if the connection has been closed
So my company is relatively new to Spring. This will be our first production implementation of the framework, and as such, we're having some of the usual pains of moving to a new method of doing things.

Anyway, this new application is a replacement for our old password manager, so it needs to be accessible via client at basically any time. The issue we're having is that our front-end connection gets abandoned because of no activity for too long, as we'd like it to. The trouble comes in when we want to get the connection back, say when a user returns to their workstation in the morning. I've looked around for days and can't seem to find anything helpful, but maybe it's easier to fix than it seems.

Database config:

```<code>    private String _getConnectionUrl() {
    /* mysql connections on tomcat crash overnight.
     * Probably need to investigate autoreconnect
     * https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html
     */
    return String.format("jdbc:mysql://%s:3306/%s",_dbHost,_dbSchema);
}

/**
 * gets the datasource associated with this connection
 * @return
 */
public DataSource getDataSource() {

    if(_dataSource==null) {
        _log.info(String.format("Creating DataSource to %s",_getConnectionUrl()));

         PoolProperties p = new PoolProperties();
          p.setUrl(_getConnectionUrl());
          p.setDriverClassName("com.mysql.jdbc.Driver");
          p.setUsername(_dbUsername);
          p.setPassword(_dbPassword);
          p.setJmxEnabled(true);
          p.setTestWhileIdle(false);
          p.setTestOnBorrow(true);
          p.setValidationQuery("SELECT 1");
          p.setTestOnReturn(false);
          p.setValidationInterval(30000);
          p.setTimeBetweenEvictionRunsMillis(30000);
          p.setMaxActive(100);
          p.setInitialSize(10);
          p.setMaxWait(10000);
          p.setRemoveAbandonedTimeout(60);
          p.setMinEvictableIdleTimeMillis(30000);
          p.setMinIdle(10);
          p.setLogAbandoned(true);
          p.setRemoveAbandoned(true);
          p.setJdbcInterceptors(
            "org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;"+
            "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer;" +
            "org.apache.tomcat.jdbc.pool.interceptor.ResetAbandonedTimer");
        _dataSource = new DataSource();
        _dataSource.setPoolProperties(p);
    }

    return _dataSource;

}
</code>```

We are using a hibernate as the JPA engine, and MySQL5 as the database.

I can provide more code upon request.

Stack trace for error after connection has been abandoned:

```<code>java.sql.SQLException: Connection has already been closed.
at org.apache.tomcat.jdbc.pool.ProxyConnection.invoke(ProxyConnection.java:117) ~[tomcat-jdbc-8.5.15.jar:na]
at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:108) ~[tomcat-jdbc-8.5.15.jar:na]
at org.apache.tomcat.jdbc.pool.interceptor.AbstractCreateStatementInterceptor.invoke(AbstractCreateStatementInterceptor.java:79) ~[tomcat-jdbc-8.5.15.jar:na]
at org.apache.tomcat.jdbc.pool.interceptor.ResetAbandonedTimer.invoke(ResetAbandonedTimer.java:60) ~[tomcat-jdbc-8.5.15.jar:na]
at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:108) ~[tomcat-jdbc-8.5.15.jar:na]
at org.apache.tomcat.jdbc.pool.interceptor.AbstractCreateStatementInterceptor.invoke(AbstractCreateStatementInterceptor.java:79) ~[tomcat-jdbc-8.5.15.jar:na]
at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:108) ~[tomcat-jdbc-8.5.15.jar:na]
at org.apache.tomcat.jdbc.pool.interceptor.ConnectionState.invoke(ConnectionState.java:152) ~[tomcat-jdbc-8.5.15.jar:na]
at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:108) ~[tomcat-jdbc-8.5.15.jar:na]
at org.apache.tomcat.jdbc.pool.TrapException.invoke(TrapException.java:40) ~[tomcat-jdbc-8.5.15.jar:na]
at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:108) ~[tomcat-jdbc-8.5.15.jar:na]
at org.apache.tomcat.jdbc.pool.interceptor.AbstractCreateStatementInterceptor.invoke(AbstractCreateStatementInterceptor.java:79) ~[tomcat-jdbc-8.5.15.jar:na]
at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:108) ~[tomcat-jdbc-8.5.15.jar:na]
at org.apache.tomcat.jdbc.pool.DisposableConnectionFacade.invoke(DisposableConnectionFacade.java:81) ~[tomcat-jdbc-8.5.15.jar:na]
at com.sun.proxy.$Proxy67.setAutoCommit(Unknown Source) ~[na:na]
at org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor.begin(AbstractLogicalConnectionImplementor.java:67) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.begin(LogicalConnectionManagedImpl.java:238) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.begin(JdbcResourceLocalTransactionCoordinatorImpl.java:214) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.engine.transaction.internal.TransactionImpl.begin(TransactionImpl.java:52) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1512) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.internal.TransactionImpl.begin(TransactionImpl.java:45) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.swecunet.portal.PortalJPADatabase.save(PortalJPADatabase.java:92) ~[classes/:na]
at org.swecunet.portal.pwmgr.PWMController.passwordGet(PWMController.java:152) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_144]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_144]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_144]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_144]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97) ~[spring-webmvc-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) ~[spring-webmvc-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) ~[spring-webmvc-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) ~[spring-webmvc-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) ~[spring-webmvc-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) ~[spring-webmvc-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) ~[spring-webmvc-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.15.jar:8.5.15]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.springframework.security.kerberos.web.authentication.SpnegoAuthenticationProcessingFilter.doFilter(SpnegoAuthenticationProcessingFilter.java:128) ~[spring-security-kerberos-web-1.0.1.RELEASE.jar:1.0.1.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:317) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:115) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:169) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:121) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.csrf.CsrfFilter.doFilterInternal(CsrfFilter.java:100) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:66) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:105) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478) [tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80) [tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799) [tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861) [tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455) [tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.15.jar:8.5.15]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_144]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_144]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.15.jar:8.5.15]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_144]
</code>```

Edit -
Persistence settings defined in our JPA connection class:

```<code>    private EntityManager _entityManager;
/**
 * gets the JPA entity manager associated with this database connection
 * @return
 */
public EntityManager getEntityManager() {
    if(_entityManager==null) {
        _entityManager=getEntityManagerFactory().createEntityManager();
    }
    return _entityManager;

}

/**
 * Save an entity into this database
 * @param entityObjectToSave the @Entity jpa object within the model package namespace
 */
public void save(Object entityObjectToSave) {
    getEntityManager().getTransaction().begin();
    getEntityManager().persist(entityObjectToSave);
    getEntityManager().getTransaction().commit();
}

/**
 * Delete an entity from this database
 * @param entityObjectToDelete the @Entity jpa object within the model package namespace
 */
public void remove(Object entityObjectToDelete) {
    getEntityManager().getTransaction().begin();
    getEntityManager().remove(entityObjectToDelete);
    getEntityManager().getTransaction().commit();
}

private EntityManagerFactory _entityManagerFactory;

public EntityManagerFactory getEntityManagerFactory() {
    if(_entityManagerFactory==null) {

        final LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
        emf.setDataSource(getDataSource());
        emf.setPersistenceUnitName(_dbSchema);
        _log.info(String.format("Scanning %s for entity models...", _dbJPAPackageLocation));
        emf.setPackagesToScan(_dbJPAPackageLocation);
        HibernateJpaVendorAdapter hjpa=new HibernateJpaVendorAdapter();
        hjpa.setShowSql(_dbJPAShowSQL);
        emf.setJpaVendorAdapter(hjpa);
        Properties properties = new Properties();
        properties.setProperty("spring.jpa.hibernate.ddl-auto", "update");
        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
        emf.setJpaProperties(properties);
        emf.setPersistenceProviderClass(HibernatePersistenceProvider.class);
        emf.afterPropertiesSet();
        _entityManagerFactory=emf.getObject();
    }
    return _entityManagerFactory;
}
</code>```

答:
Hibernate has a bug with maintaining a connection for a long period, that C3P0 fixes.

Try adding these to your persistence.xml

```<code>      <property name="hibernate.connection.provider_class"
      value="org.hibernate.connection.C3P0ConnectionProvider" />

  <property name="hibernate.c3p0.max_size" value="5" />
  <property name="hibernate.c3p0.min_size" value="0" />
  <property name="hibernate.c3p0.acquire_increment" value="1" />
  <property name="hibernate.c3p0.idle_test_period" value="300"/>
  <property name="hibernate.c3p0.max_statements" value="0" />
  <property name="hibernate.c3p0.timeout" value="60" />
  <property name="hibernate.connection.zeroDateTimeBehavior" value="convertToNull"></property>
</code>```

Body:
So my company is relatively new to Spring. This will be our first production implementation of the framework, and as such, we're having some of the usual pains of moving to a new method of doing things.

Anyway, this new application is a replacement for our old password manager, so it needs to be accessible via client at basically any time. The issue we're having is that our front-end connection gets abandoned because of no activity for too long, as we'd like it to. The trouble comes in when we want to get the connection back, say when a user returns to their workstation in the morning. I've looked around for days and can't seem to find anything helpful, but maybe it's easier to fix than it seems.

Database config:

    private String _getConnectionUrl() {
    /* mysql connections on tomcat crash overnight.
     * Probably need to investigate autoreconnect
     * https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html
     */
    return String.format("jdbc:mysql://%s:3306/%s",_dbHost,_dbSchema);
}

/**
 * gets the datasource associated with this connection
 * @return
 */
public DataSource getDataSource() {

    if(_dataSource==null) {
        _log.info(String.format("Creating DataSource to %s",_getConnectionUrl()));

         PoolProperties p = new PoolProperties();
          p.setUrl(_getConnectionUrl());
          p.setDriverClassName("com.mysql.jdbc.Driver");
          p.setUsername(_dbUsername);
          p.setPassword(_dbPassword);
          p.setJmxEnabled(true);
          p.setTestWhileIdle(false);
          p.setTestOnBorrow(true);
          p.setValidationQuery("SELECT 1");
          p.setTestOnReturn(false);
          p.setValidationInterval(30000);
          p.setTimeBetweenEvictionRunsMillis(30000);
          p.setMaxActive(100);
          p.setInitialSize(10);
          p.setMaxWait(10000);
          p.setRemoveAbandonedTimeout(60);
          p.setMinEvictableIdleTimeMillis(30000);
          p.setMinIdle(10);
          p.setLogAbandoned(true);
          p.setRemoveAbandoned(true);
          p.setJdbcInterceptors(
            "org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;"+
            "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer;" +
            "org.apache.tomcat.jdbc.pool.interceptor.ResetAbandonedTimer");
        _dataSource = new DataSource();
        _dataSource.setPoolProperties(p);
    }

    return _dataSource;

}

We are using a hibernate as the JPA engine, and MySQL5 as the database.

I can provide more code upon request.

Stack trace for error after connection has been abandoned:

java.sql.SQLException: Connection has already been closed.
at org.apache.tomcat.jdbc.pool.ProxyConnection.invoke(ProxyConnection.java:117) ~[tomcat-jdbc-8.5.15.jar:na]
at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:108) ~[tomcat-jdbc-8.5.15.jar:na]
at org.apache.tomcat.jdbc.pool.interceptor.AbstractCreateStatementInterceptor.invoke(AbstractCreateStatementInterceptor.java:79) ~[tomcat-jdbc-8.5.15.jar:na]
at org.apache.tomcat.jdbc.pool.interceptor.ResetAbandonedTimer.invoke(ResetAbandonedTimer.java:60) ~[tomcat-jdbc-8.5.15.jar:na]
at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:108) ~[tomcat-jdbc-8.5.15.jar:na]
at org.apache.tomcat.jdbc.pool.interceptor.AbstractCreateStatementInterceptor.invoke(AbstractCreateStatementInterceptor.java:79) ~[tomcat-jdbc-8.5.15.jar:na]
at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:108) ~[tomcat-jdbc-8.5.15.jar:na]
at org.apache.tomcat.jdbc.pool.interceptor.ConnectionState.invoke(ConnectionState.java:152) ~[tomcat-jdbc-8.5.15.jar:na]
at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:108) ~[tomcat-jdbc-8.5.15.jar:na]
at org.apache.tomcat.jdbc.pool.TrapException.invoke(TrapException.java:40) ~[tomcat-jdbc-8.5.15.jar:na]
at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:108) ~[tomcat-jdbc-8.5.15.jar:na]
at org.apache.tomcat.jdbc.pool.interceptor.AbstractCreateStatementInterceptor.invoke(AbstractCreateStatementInterceptor.java:79) ~[tomcat-jdbc-8.5.15.jar:na]
at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:108) ~[tomcat-jdbc-8.5.15.jar:na]
at org.apache.tomcat.jdbc.pool.DisposableConnectionFacade.invoke(DisposableConnectionFacade.java:81) ~[tomcat-jdbc-8.5.15.jar:na]
at com.sun.proxy.$Proxy67.setAutoCommit(Unknown Source) ~[na:na]
at org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor.begin(AbstractLogicalConnectionImplementor.java:67) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.begin(LogicalConnectionManagedImpl.java:238) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.begin(JdbcResourceLocalTransactionCoordinatorImpl.java:214) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.engine.transaction.internal.TransactionImpl.begin(TransactionImpl.java:52) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1512) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.internal.TransactionImpl.begin(TransactionImpl.java:45) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.swecunet.portal.PortalJPADatabase.save(PortalJPADatabase.java:92) ~[classes/:na]
at org.swecunet.portal.pwmgr.PWMController.passwordGet(PWMController.java:152) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_144]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_144]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_144]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_144]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97) ~[spring-webmvc-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) ~[spring-webmvc-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) ~[spring-webmvc-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) ~[spring-webmvc-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) ~[spring-webmvc-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) ~[spring-webmvc-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) ~[spring-webmvc-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.15.jar:8.5.15]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.springframework.security.kerberos.web.authentication.SpnegoAuthenticationProcessingFilter.doFilter(SpnegoAuthenticationProcessingFilter.java:128) ~[spring-security-kerberos-web-1.0.1.RELEASE.jar:1.0.1.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:317) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:115) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:169) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:121) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.csrf.CsrfFilter.doFilterInternal(CsrfFilter.java:100) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:66) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177) ~[spring-security-web-4.1.4.RELEASE.jar:4.1.4.RELEASE]
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:105) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) ~[tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478) [tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80) [tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799) [tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861) [tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455) [tomcat-embed-core-8.5.15.jar:8.5.15]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.15.jar:8.5.15]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_144]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_144]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.15.jar:8.5.15]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_144]

Edit -
Persistence settings defined in our JPA connection class:

    private EntityManager _entityManager;
/**
 * gets the JPA entity manager associated with this database connection
 * @return
 */
public EntityManager getEntityManager() {
    if(_entityManager==null) {
        _entityManager=getEntityManagerFactory().createEntityManager();
    }
    return _entityManager;

}

/**
 * Save an entity into this database
 * @param entityObjectToSave the @Entity jpa object within the model package namespace
 */
public void save(Object entityObjectToSave) {
    getEntityManager().getTransaction().begin();
    getEntityManager().persist(entityObjectToSave);
    getEntityManager().getTransaction().commit();
}

/**
 * Delete an entity from this database
 * @param entityObjectToDelete the @Entity jpa object within the model package namespace
 */
public void remove(Object entityObjectToDelete) {
    getEntityManager().getTransaction().begin();
    getEntityManager().remove(entityObjectToDelete);
    getEntityManager().getTransaction().commit();
}

private EntityManagerFactory _entityManagerFactory;

public EntityManagerFactory getEntityManagerFactory() {
    if(_entityManagerFactory==null) {

        final LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
        emf.setDataSource(getDataSource());
        emf.setPersistenceUnitName(_dbSchema);
        _log.info(String.format("Scanning %s for entity models...", _dbJPAPackageLocation));
        emf.setPackagesToScan(_dbJPAPackageLocation);
        HibernateJpaVendorAdapter hjpa=new HibernateJpaVendorAdapter();
        hjpa.setShowSql(_dbJPAShowSQL);
        emf.setJpaVendorAdapter(hjpa);
        Properties properties = new Properties();
        properties.setProperty("spring.jpa.hibernate.ddl-auto", "update");
        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
        emf.setJpaProperties(properties);
        emf.setPersistenceProviderClass(HibernatePersistenceProvider.class);
        emf.afterPropertiesSet();
        _entityManagerFactory=emf.getObject();
    }
    return _entityManagerFactory;
}

Body:
Hibernate has a bug with maintaining a connection for a long period, that C3P0 fixes.

Try adding these to your persistence.xml

      <property name="hibernate.connection.provider_class"
      value="org.hibernate.connection.C3P0ConnectionProvider" />

  <property name="hibernate.c3p0.max_size" value="5" />
  <property name="hibernate.c3p0.min_size" value="0" />
  <property name="hibernate.c3p0.acquire_increment" value="1" />
  <property name="hibernate.c3p0.idle_test_period" value="300"/>
  <property name="hibernate.c3p0.max_statements" value="0" />
  <property name="hibernate.c3p0.timeout" value="60" />
  <property name="hibernate.connection.zeroDateTimeBehavior" value="convertToNull"></property>


来源: stackexchange

问:
creating a dictionary with two lists when a key should have two values in python
hi I have two lists,

```<code>list1
['PO', 'NA', 'PO']

list2
['Post Office', 'Not Available', 'Post area']
</code>```

Iam trying to get a dictionary using this two lists
I tried, my_dict=dict(zip(list1,list2))
it gives,

```<code> {'NA': 'Not Available', 'PO': 'Post area'}
</code>```

but my expected output is,

```<code> {'NA': 'Not Available', 'PO': ['Post area','Post Office']}
</code>```

pls help

答:
There are two method :

First method using defaultdict:

```<code>from collections import defaultdict

list1=['PO', 'NA', 'PO']

list2=['Post Office', 'Not Available', 'Post area']

final_dict = defaultdict(list)

[final_dict[item[0]].append(item[1]) for item in zip(list1,list2)]
print(final_dict)
</code>```

output:

```<code>defaultdict(<class 'list'>, {'PO': ['Post Office', 'Post area'], 'NA': ['Not Available']})
</code>```

Second method :

Let's solve your issue in two step:

First zip the both list:

```<code>list1=['PO', 'NA', 'PO']

list2=['Post Office', 'Not Available', 'Post area']

zip_list=[item for item in zip(list1,list2)]
</code>```

Now check if zip tuple first element is in dict or not if it is then
  use this awesome pattern :

```<code>final_dict={}
    for item in zip_list:
        if item[0] not in final_dict:
            final_dict[item[0]]=[item[1]]
        else:
            final_dict[item[0]].append(item[1])
</code>```

Full code:

```<code>list1=['PO', 'NA', 'PO']

list2=['Post Office', 'Not Available', 'Post area']

zip_list=[item for item in zip(list1,list2)]
final_dict={}
for item in zip_list:
    if item[0] not in final_dict:
        final_dict[item[0]]=[item[1]]
    else:
        final_dict[item[0]].append(item[1])

print(final_dict)
</code>```

output:

```<code>{'NA': ['Not Available'], 'PO': ['Post Office', 'Post area']}
</code>```

ContentLicense: CC BY-SA 3.0

Body:
hi I have two lists,

list1
['PO', 'NA', 'PO']

list2
['Post Office', 'Not Available', 'Post area']

Iam trying to get a dictionary using this two lists
I tried, my_dict=dict(zip(list1,list2))
it gives,

 {'NA': 'Not Available', 'PO': 'Post area'}

but my expected output is,

 {'NA': 'Not Available', 'PO': ['Post area','Post Office']}

pls help

Body:
There are two method :

First method using defaultdict:

from collections import defaultdict

list1=['PO', 'NA', 'PO']

list2=['Post Office', 'Not Available', 'Post area']

final_dict = defaultdict(list)

[final_dict[item[0]].append(item[1]) for item in zip(list1,list2)]
print(final_dict)

output:

defaultdict(<class 'list'>, {'PO': ['Post Office', 'Post area'], 'NA': ['Not Available']})

Second method :

Let's solve your issue in two step:

First zip the both list:

list1=['PO', 'NA', 'PO']

list2=['Post Office', 'Not Available', 'Post area']

zip_list=[item for item in zip(list1,list2)]

Now check if zip tuple first element is in dict or not if it is then
  use this awesome pattern :

final_dict={}
    for item in zip_list:
        if item[0] not in final_dict:
            final_dict[item[0]]=[item[1]]
        else:
            final_dict[item[0]].append(item[1])

Full code:

list1=['PO', 'NA', 'PO']

list2=['Post Office', 'Not Available', 'Post area']

zip_list=[item for item in zip(list1,list2)]
final_dict={}
for item in zip_list:
    if item[0] not in final_dict:
        final_dict[item[0]]=[item[1]]
    else:
        final_dict[item[0]].append(item[1])

print(final_dict)

output:

{'NA': ['Not Available'], 'PO': ['Post Office', 'Post area']}


问:
How to use Selenium webdriver to execute test cases in Testlink
Could someone please explain how to link the webdriver scripts [tests written in eclipse] to test link tests and update results accordingly?

for Example my tests will look like this in my webdriver test plan [One class having all my tests]

```<code>@Test
testA
{
}

@Test
testB
{
}
@Test
testC
{
}
</code>```

I have corresponding test cases defined in my test link database for testA, testB, testC.

When I run the script , the tests in  test link DB need to be updated accordingly based on PASS/FAIL criteria.

The env am using from my tests is

```<code>eclipse [for developing     webdriver     scripts]
selenium 2.0
testlink
Testng
1234
</code>```

答:
```<code>        @Prasad I am using assertion before creating result like this:

      try {

          driver.navigate().to("http://www.wikipedia.org/wiki/Main_Page");
           Assert.assertEquals(ActualTitle, ExpextedTitle);
          result = TestLinkAPIResults.TEST_PASSED;
          updateTestLinkResult("AT-1", null, result);

          }
           catch (Exception ex)
        {
          result = TestLinkAPIResults.TEST_FAILED;
          exception = ex.getMessage();
          updateTestLinkResult("AT-1", exception, result);
             }

      But I don't know why result are not showing in testlink. Can you
      please tell me better approach to use Assertion here.
</code>```

Body:
Could someone please explain how to link the webdriver scripts [tests written in eclipse] to test link tests and update results accordingly?

for Example my tests will look like this in my webdriver test plan [One class having all my tests]

@Test
testA
{
}

@Test
testB
{
}
@Test
testC
{
}

I have corresponding test cases defined in my test link database for testA, testB, testC.

When I run the script , the tests in  test link DB need to be updated accordingly based on PASS/FAIL criteria.

The env am using from my tests is

eclipse [for developing     webdriver     scripts]
selenium 2.0
testlink
Testng
1234


问:
android getting pictures from drawables
Bitmap picture = BitmapFactory.decodeResource(getResources(),R.drawable.phscale);

apart from above code any other ways to get the bitmap from drawables

答:
See th link
How to convert a Drawable to a Bitmap?</p>

Title: android getting pictures from drawables

ContentLicense: CC BY-SA 2.5


问:
How to hide ide main window title bar? (in IntelliJ IDEA)
Title bar is completely useless for me. Info it shows i can see in navigation bar.
It only takes space. Is it possible to hide the header as looks in Netbeans?

答:
install plugin: Window Decorate Mode Switcher

select view->switch decorate mode

You might want to toggle the "Auto undecorate on start" option under File > Settings > Switch Decorate Mode

ContentLicense: CC BY-SA 4.0


问:
Django Attribute error: "unicode object has no attribute tags"
Everything works quite well,so I presumed anyways but when I runserver and click the title of my blog,i get an AttributeError saying 'unicode object has no attribute tags'

heres the the piece of code handling the tags

```<code>def post_detail(request, year, month, day, post):
     post_tags_ids = post.tags.values_list('id', flat=True)
     similar_posts =  Post.published.filter(tags__in=post_tags_ids).exclude(id=post.id)
     similar_posts =  similar_posts.annotate(same_tags=Count('tags')).order_by('-same_tags','-publish')[:4]
     post = get_object_or_404(Post, slug=post,status='published',publish__year=year,publish__month=month,publish__day=day)
     return render(request,'blog/post/detail.html',{'post': post,'comments': comments,'comment_form': comment_form,'similar_posts': similar_posts})
</code>```

答:
It seems that the 'post' argument you are passing is not a model instance as you presume. I think you need to fetch the post object first. Assuming you are passing pk as post:</p>

```<code>post_object = get_object_or_404(Post, pk=post)
post_tags_ids = post_object.tags.values_list('id', flat=True)
</code>```

Body:
Everything works quite well,so I presumed anyways but when I runserver and click the title of my blog,i get an AttributeError saying 'unicode object has no attribute tags'

heres the the piece of code handling the tags

def post_detail(request, year, month, day, post):
     post_tags_ids = post.tags.values_list('id', flat=True)
     similar_posts =  Post.published.filter(tags__in=post_tags_ids).exclude(id=post.id)
     similar_posts =  similar_posts.annotate(same_tags=Count('tags')).order_by('-same_tags','-publish')[:4]
     post = get_object_or_404(Post, slug=post,status='published',publish__year=year,publish__month=month,publish__day=day)
     return render(request,'blog/post/detail.html',{'post': post,'comments': comments,'comment_form': comment_form,'similar_posts': similar_posts})

Body:
It seems that the 'post' argument you are passing is not a model instance as you presume. I think you need to fetch the post object first. Assuming you are passing pk as post:

post_object = get_object_or_404(Post, pk=post)
post_tags_ids = post_object.tags.values_list('id', flat=True)


问:
Kamailio and rtpproxy error and why do we need rtpproxy (example that works without it)
I am using Kamailio version 5.0.3 (x86_64/linux), and it is installed and working on a Centos 7 Sever.  I use Kamailio as a SIP Proxy Server for Asterisk and things seem to work fine, however, I do have an issue and a question for which I am trying to understand and am hoping someone can help me with.

For the sake of the argument, my server name is myServer, and it’s IP Address is 172.1.1.13; Asterisk is on a different server.

Issue:  Every 10 seconds I get an Error message in the Kamailio log which says =>
Nov 13 18:10:55 myServer /usr/local/sbin/kamailio[29188]: ERROR:  [core/tcp_main.c:2660]: tcpconn_1st_send(): connect 172.1.1.13:59794 failed (RST) Connection refused.

I do not know what triggers this error, and as mentioned, things do seem to work; I can make calls and use sip tools which pass requests through the Kamailio to Asterisk.  Does anyone know what could be causing this Error?  I do not understand what is trying to connect to what at such a high port.

Question: The way I understand it, rtpproxy is needed to pass video/audio through Kamailio to Asterisk.  However, even without rtpproxy running, I get video through to Asterisk and the video plays with no issues on a sip device (e.g. MicroSIP).  I am confused what I need rtpproxy for.
Note: Even with no rtpproxy, I get the same error mentioned above in the Issue section.

Thank you,

答:
This seems like you have mentioned this connection port for some service within kamailio.cfg , so you should look into your kamailio.cfg and see why is it connection to 172.1.1.13:59794 and you'll know to why it's trying to connect .

RTPProxy or RtpEngine is used for the Relaying purposes and it can also do the crypto things between legacy SIP client and webRTC clients, however the latest module can also record the calls as .pcap or .mp3/.wav


问:
Count vowels in a string with ruby
My objective is to return the number of vowels in a given string. I've tried this code:

```<code>def count_vowels(string)
  vowels = ['a', 'e', 'i', 'o', 'u']
  chars_ary = string.split(//)
  ary_with_vowels = chars_ary.take_while {|letter| vowels.include?(letter)}
  return ary_with_vowels.length
end
</code>```

and it doesn't pass the majority of test cases. I understand that there are multiple other ways to solve this problem, but I want to solve it using the code that I provided.

Can someone give me some insight as to why this isn't working?

答:
This way is easier:</p>

```<code> def count_vowels(string)
   string.count('aeiou')
 end
</code>```

Title: Count vowels in a string with ruby

Body:
My objective is to return the number of vowels in a given string. I've tried this code:

def count_vowels(string)
  vowels = ['a', 'e', 'i', 'o', 'u']
  chars_ary = string.split(//)
  ary_with_vowels = chars_ary.take_while {|letter| vowels.include?(letter)}
  return ary_with_vowels.length
end

and it doesn't pass the majority of test cases. I understand that there are multiple other ways to solve this problem, but I want to solve it using the code that I provided.

Can someone give me some insight as to why this isn't working?

Body:
This way is easier:

 def count_vowels(string)
   string.count('aeiou')
 end


问:
How to get the id of the td based if the text value matches exactly
How to get the id of the td based if the text value matches exactly?

    George
    George2
    George3
    Georg

```

I want the id of "Georg" td4 to get returned?

答:
You need to use filter() method to get the element.

console.log(
  // get all td with id attribute
  // or additionally use :contains to get more specific
  // $('td[id]:contains("Georg")')
  $('td[id]')
  // filter the jquery element collection
  .filter(function() {
    // compare content
    return $(this).text().trim() === 'Georg';
  })
  // get the id of filtered element
  .attr('id')
);```
<pre class="snippet-code-html lang-html prettyprint-override"><code><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="tid1">
  <tr>
    <td id="td1">George</td>
    <td id="td2">George2</td>
    <td id="td3">George3</td>
    <td id="td4">Georg</td>

  </tr>
</table></code>```

Body:
How to get the id of the td based if the text value matches exactly?

    George
    George2
    George3
    Georg

I want the id of "Georg" td4 to get returned?

Body:
You need to use filter() method to get the element.

console.log(
  // get all td with id attribute
  // or additionally use :contains to get more specific
  // $('td[id]:contains("Georg")')
  $('td[id]')
  // filter the jquery element collection
  .filter(function() {
    // compare content
    return $(this).text().trim() === 'Georg';
  })
  // get the id of filtered element
  .attr('id')
);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="tid1">
  <tr>
    <td id="td1">George</td>
    <td id="td2">George2</td>
    <td id="td3">George3</td>
    <td id="td4">Georg</td>

  </tr>
</table>


答:
You can use a dynamic query to make this.

```<code>DECLARE @ColumnNames NVARCHAR(MAX) =''
SELECT  @ColumnNames = @ColumnNames + ', ' + QUOTENAME ( ColName + ' Value' ) + ', ' + QUOTENAME ( ColName +' Score' )  + ', ' + QUOTENAME ( ColName +' Comments' )
FROM (SELECT DISTINCT CONVERT(VARCHAR,Aslvl_ID) ColName, Aslvl_ID FROM ParamDetails) AS T ORDER BY Aslvl_ID
SET @ColumnNames = STUFF(@ColumnNames,1,1,'')

DECLARE @SqlText NVARCHAR(MAX)
SET @SqlText  = 'SELECT * FROM
                    (SELECT ROW_NUMBER() OVER(PARTITION BY Aslvl_ID ORDER BY Aslvl_ID) RN,  CONVERT(VARCHAR,Aslvl_ID) +'' Value'' Col, CONVERT(VARCHAR,Value) Val FROM ParamDetails
                       UNION ALL
                    SELECT  ROW_NUMBER() OVER(PARTITION BY Aslvl_ID ORDER BY Aslvl_ID) RN, CONVERT(VARCHAR,Aslvl_ID) +'' Score'' Col, CONVERT(VARCHAR,Score) Val FROM ParamDetails
                       UNION ALL
                    SELECT  ROW_NUMBER() OVER(PARTITION BY Aslvl_ID ORDER BY Aslvl_ID ) RN, CONVERT(VARCHAR,Aslvl_ID) +'' Comments'' Col, Comments Val FROM ParamDetails
                    ) SRC
                    PIVOT (MAX(Val) FOR Col IN (' + @ColumnNames + ') ) PVT'

EXEC sp_executesql @SqlText
</code>```

Result:

```<code>RN  1 Value          1 Score          1 Comments       6 Value          6 Score          6 Comments       11 Value         11 Score         11 Comments
--- ---------------- ---------------- ---------------- ---------------- ---------------- ---------------- ---------------- ---------------- -----------------------
1   123.0000         12.3000          Usha P-6914      147.0000         14.7000          Bhaskar L-17957  4569.0000        456.9000         Murali Prasad K-21632
2   321.0000         64.2000          Usha P-6914      741.0000         148.2000         Bhaskar L-17957  9654.0000        1930.8000        Murali Prasad K-21632
3   456.0000         136.8000         Usha P-6914      258.0000         77.4000          Bhaskar L-17957  4789.0000        1436.7000        Murali Prasad K-21632
4   654.0000         261.6000         Usha P-6914      852.0000         340.8000         Bhaskar L-17957  9874.0000        3949.6000        Murali Prasad K-21632
</code>```

Title: Pivot data based on particular ID without hardcoding

Body:
I have a table called ParamDetails with the following data

Aslvl_ID    Value   Score      Comments
1        123.0000   12.3000     Usha P-6914
1        321.0000   64.2000     Usha P-6914
1        456.0000   136.8000    Usha P-6914
1        654.0000   261.6000    Usha P-6914

6        147.0000   14.7000     Bhaskar L-17957
6        741.0000   148.2000    Bhaskar L-17957
6        258.0000   77.4000     Bhaskar L-17957
6        852.0000   340.8000    Bhaskar L-17957

11       4569.0000  456.9000    Murali Prasad K-21632
11       9654.0000  1930.8000   Murali Prasad K-21632
11       4789.0000  1436.7000   Murali Prasad K-21632
11       9874.0000  3949.6000   Murali Prasad K-21632

the requirement is to write a query to display data in below format.i have tried pivot and other few things but did not find any success.
please help me with query or suggestion to get the expected output.

expected output:

1 Value    1 score     1 Comments    6 Value    6 score     6 Comments        11 Value    11 score     11 Comments
123.0000    12.3000     Usha P-6914  147.0000   14.7000     Bhaskar L-17957   4569.0000  456.9000    Murali Prasad K-21632
321.0000    64.2000     Usha P-6914  741.0000   148.2000    Bhaskar L-17957   9654.0000  1930.8000   Murali Prasad K-21632
456.0000    136.8000    Usha P-6914  258.0000   77.4000     Bhaskar L-17957   4789.0000  1436.7000   Murali Prasad K-21632
654.0000    261.6000    Usha P-6914  852.0000   340.8000    Bhaskar L-17957   9874.0000  3949.6000   Murali Prasad K-21632

Body:
You can use a dynamic query to make this.

DECLARE @ColumnNames NVARCHAR(MAX) =''
SELECT  @ColumnNames = @ColumnNames + ', ' + QUOTENAME ( ColName + ' Value' ) + ', ' + QUOTENAME ( ColName +' Score' )  + ', ' + QUOTENAME ( ColName +' Comments' )
FROM (SELECT DISTINCT CONVERT(VARCHAR,Aslvl_ID) ColName, Aslvl_ID FROM ParamDetails) AS T ORDER BY Aslvl_ID
SET @ColumnNames = STUFF(@ColumnNames,1,1,'')

DECLARE @SqlText NVARCHAR(MAX)
SET @SqlText  = 'SELECT * FROM
                    (SELECT ROW_NUMBER() OVER(PARTITION BY Aslvl_ID ORDER BY Aslvl_ID) RN,  CONVERT(VARCHAR,Aslvl_ID) +'' Value'' Col, CONVERT(VARCHAR,Value) Val FROM ParamDetails
                       UNION ALL
                    SELECT  ROW_NUMBER() OVER(PARTITION BY Aslvl_ID ORDER BY Aslvl_ID) RN, CONVERT(VARCHAR,Aslvl_ID) +'' Score'' Col, CONVERT(VARCHAR,Score) Val FROM ParamDetails
                       UNION ALL
                    SELECT  ROW_NUMBER() OVER(PARTITION BY Aslvl_ID ORDER BY Aslvl_ID ) RN, CONVERT(VARCHAR,Aslvl_ID) +'' Comments'' Col, Comments Val FROM ParamDetails
                    ) SRC
                    PIVOT (MAX(Val) FOR Col IN (' + @ColumnNames + ') ) PVT'

EXEC sp_executesql @SqlText

Result:

RN  1 Value          1 Score          1 Comments       6 Value          6 Score          6 Comments       11 Value         11 Score         11 Comments
--- ---------------- ---------------- ---------------- ---------------- ---------------- ---------------- ---------------- ---------------- -----------------------
1   123.0000         12.3000          Usha P-6914      147.0000         14.7000          Bhaskar L-17957  4569.0000        456.9000         Murali Prasad K-21632
2   321.0000         64.2000          Usha P-6914      741.0000         148.2000         Bhaskar L-17957  9654.0000        1930.8000        Murali Prasad K-21632
3   456.0000         136.8000         Usha P-6914      258.0000         77.4000          Bhaskar L-17957  4789.0000        1436.7000        Murali Prasad K-21632
4   654.0000         261.6000         Usha P-6914      852.0000         340.8000         Bhaskar L-17957  9874.0000        3949.6000        Murali Prasad K-21632


问:
How to align  value to center vertically?
I need to align td cell value to center, both horizontally and vertically.  has a rowspan attribute.

The output right now is like:

```<code>A  |  B  |  C  |  D
1  |  2  |  3  |  4
1  |  2  |  3  |
1  |  2  |  3  |
</code>```

Desired:

```<code>A  |  B  |  C  |  D
1  |  2  |  3  |
1  |  2  |  3  |  4
1  |  2  |  3  |
1  |  2  |  3  |
</code>```

答:
Try this:</p>

```<code><td rowspan="4" tyle="text-align:center;">4</td>
</code>```

Title: How to align <td rowspan ="value"> value to center vertically?

Body:
I need to align td cell value to center, both horizontally and vertically.  has a rowspan attribute.

The output right now is like:

A  |  B  |  C  |  D
1  |  2  |  3  |  4
1  |  2  |  3  |
1  |  2  |  3  |

Desired:

A  |  B  |  C  |  D
1  |  2  |  3  |
1  |  2  |  3  |  4
1  |  2  |  3  |
1  |  2  |  3  |

Body:
Try this:

<td rowspan="4" tyle="text-align:center;">4</td>


问:
cv2 not showing any thing
i am trying to use the laptop camera to take pictures and show the pictures, and i am using python 3.6.3 and windows
i have tried to use the cv2 module and i have used this simple code

```<code>import  cv2
video=cv2.VideoCapture(0)
print ( video.read())
video.release()
</code>```

and it only receives black picture , this is what i got form the print ( video.read()) :

```<code>(True, array([[[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       ...,
       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]]], dtype=uint8))
</code>```

i have tried different codes and i have tried to change video=cv2.VideoCapture(0) to video=cv2.VideoCapture(1) or to video=cv2.VideoCapture(-1)
and did not work when i changed it to 1 or -1 it printed (False , none) and also i am sure all my drivers don't need any update

is there is any other ways to take pictures from my laptop camera ? other then the cv2 and pygame ? as long as pygame camera only work for linux

答:
This is the way I do it and works without a problem:

```<code>video_capture = cv2.VideoCapture(0)

while True:
    if not video_capture.isOpened():
        print('Unable to load camera.')
        sleep(5)
        pass

    # Capture frame-by-frame
    ret, frame = video_capture.read()
</code>```

What you have on the frame variable is your picture so you can save it to disk with something like:

```<code>cv2.imwrite("mypic.jpg",frame)
</code>```

Title: cv2 not showing any thing

Body:
i am trying to use the laptop camera to take pictures and show the pictures, and i am using python 3.6.3 and windows
i have tried to use the cv2 module and i have used this simple code

import  cv2
video=cv2.VideoCapture(0)
print ( video.read())
video.release()

and it only receives black picture , this is what i got form the print ( video.read()) :

(True, array([[[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       ...,
       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]]], dtype=uint8))

i have tried different codes and i have tried to change video=cv2.VideoCapture(0) to video=cv2.VideoCapture(1) or to video=cv2.VideoCapture(-1)
and did not work when i changed it to 1 or -1 it printed (False , none) and also i am sure all my drivers don't need any update

is there is any other ways to take pictures from my laptop camera ? other then the cv2 and pygame ? as long as pygame camera only work for linux

Body:
This is the way I do it and works without a problem:

video_capture = cv2.VideoCapture(0)

while True:
    if not video_capture.isOpened():
        print('Unable to load camera.')
        sleep(5)
        pass

    # Capture frame-by-frame
    ret, frame = video_capture.read()

What you have on the frame variable is your picture so you can save it to disk with something like:

cv2.imwrite("mypic.jpg",frame)


问:
Delete item list ionic slide 3
I run ionic 3 app on browser, when I slide, and click on delete it redirect on the itemDetails instead to reload the same list without the item deleted</p>

```<code><ion-list>
    <ion-item-sliding *ngFor="let item of items" (click)="itemDetails($event, item)" (ionSwipe)="delete(item)">
     <ion-item>
     {{item.name}}
     </ion-item>

     <ion-item-options>
        <button ion-button expandable (click)="removeItem(item.id)">Delete</button>
     </ion-item-options>
    </ion-item-sliding>
</ion-list>
</code>```

答:
The problem is the list of items is not getting the updated list. You have get the updated list after deleting it.

HTML

```<code><ion-list>
<ion-item-sliding *ngFor="let item of items" (click)="itemDetails($event, item)" (ionSwipe)="delete(item)">
 <ion-item>
 {{item.name}}
 </ion-item>

 <ion-item-options>
    <button ion-button expandable (click)="removeItem(item.id)">Delete</button>
 </ion-item-options>
</ion-item-sliding>
</code>```

ts

```<code>removeItem(item) {
 `your code`
  loadItems(); #get the updated list
 }
</code>```

Title: Delete item list ionic slide 3

Body:
I run ionic 3 app on browser, when I slide, and click on delete it redirect on the itemDetails instead to reload the same list without the item deleted

<ion-list>
    <ion-item-sliding *ngFor="let item of items" (click)="itemDetails($event, item)" (ionSwipe)="delete(item)">
     <ion-item>
     {{item.name}}
     </ion-item>

     <ion-item-options>
        <button ion-button expandable (click)="removeItem(item.id)">Delete</button>
     </ion-item-options>
    </ion-item-sliding>
</ion-list>

Body:
The problem is the list of items is not getting the updated list. You have get the updated list after deleting it.

HTML

<ion-list>
<ion-item-sliding *ngFor="let item of items" (click)="itemDetails($event, item)" (ionSwipe)="delete(item)">
 <ion-item>
 {{item.name}}
 </ion-item>

 <ion-item-options>
    <button ion-button expandable (click)="removeItem(item.id)">Delete</button>
 </ion-item-options>
</ion-item-sliding>

ts

removeItem(item) {
 `your code`
  loadItems(); #get the updated list
 }


答:
There is a linear programming approach to this, and you might find R packages that will do linear programming for you.

Find a space of smooth curves such as splines, where a smooth curve is a linear combination of basis functions.

Now solve the linear program to minimize the sum, taken over all points, of the value at that point minus the value from a linear combination of basis functions at that point, subject to the constraint that the linear combination of basis functions must be no larger than the value at that point.

Title: Lower Bound of a Curve

Body:
I have a curve likes the following:

As can be seen, there are two upper bound and lower bound curves on the above (see the following image for the approx visualization!):

I have tried to find the lower bound by the following code:

lower_bound = rbind()
for(i in 2:(length(data[,1])-1))
{
    if(data[i,2] < data[i+1,2] && data[i,2] < data[i-1,2])
    {
        lower_bound = rbind2(lower_bound, c(data[i,1], data[i,2]))
    }
}

But, the problem is there are some local minima in the middle, so the result is not what I've expected (as follow).

Hence the question is how can I find the lower bound of such a curve in R? Is there any known algorithm to find the lower bound?

About the minimal reproducible example, I've included 500 of data (to make sense) in the following:

structure(c(3.54246575342466, 5.75616438356164, 6.54246575342466,
9.73424657534247, 7.75616438356164, 9.75616438356164, 1.41917808219178,
2.75342465753425, 3.16986301369863, 2.27123287671233, 3.74520547945205,
5.74520547945205, 4.29315068493151, 3.37260273972603, 8.37534246575343,
3.33698630136986, 5.04657534246575, 6.92328767123288, 1.33972602739726,
6.34246575342466, 26.358904109589, 2.34794520547945, 4.35068493150685,
7.35342465753425, 27.3671232876712, 1.15068493150685, 2.14794520547945,
4.15342465753425, 6.15342465753425, 9.15616438356164, 19.1616438356164,
29.1698630136986, 2.58904109589041, 4.5972602739726, 9.6, 29.613698630137,
3.10684931506849, 8.11780821917808, 3.35068493150685, 8.37260273972603,
5.11506849315068, 28.1315068493151, 5.36986301369863, 28.386301369863,
4.87945205479452, 4.46027397260274, 5.35616438356164, 25.3698630136986,
25.8684931506849, 5.85479452054795, 1.85205479452055, 4.37260273972603,
6.37260273972603, 9.37534246575343, 19.3835616438356, 29.3890410958904,
18.3808219178082, 1.36986301369863, 8.37534246575343, 3.37260273972603,
5.85479452054795, 28.3890410958904, 2.87671232876712, 4.87945205479452,
7.37808219178082, 8.17260273972603, 28.186301369863, 4.29315068493151,
6.96164383561644, 26.3479452054795, 7.96438356164384, 1.53972602739726,
1.58630136986301, 5.04657534246575, 16.6356164383562, 18.4684931506849,
25.0602739726027, 15.6493150684932, 24.8547945205479, 25.3506849315068,
26.1068493150685, 16.0547945205479, 2.16712328767123, 19.2657534246575,
20.6849315068493, 1.04383561643836, 3.04383561643836, 23.0575342465753,
3.29315068493151, 4.13150684931507, 2.04383561643836, 22.0575342465753,
22.8904109589041, 2.87671232876712, 5.54246575342466, 25.5561643835616,
1.04931506849315, 6.05205479452055, 26.0657534246575, 2.09041095890411,
7.09315068493151, 27.1068493150685, 2.09041095890411, 4.09315068493151,
6.09315068493151, 9.0958904109589, 19.1013698630137, 29.1095890410959,
1.83287671232877, 1.42739726027397, 3.43013698630137, 9.3041095890411,
2.72328767123288, 8.58082191780822, 2.8958904109589, 4.92054794520548,
9.92328767123288, 6.92054794520548, 19.9315068493151, 29.9369863013699,
20.9315068493151, 2.25205479452055, 22.2657534246575, 3.40821917808219,
23.4219178082192, 8.21095890410959, 3.20821917808219, 5.20821917808219,
18.8027397260274, 19.8054794520548, 21.7232876712329, 2.25205479452055,
22.8054794520548, 24.4301369863014, 4.41643835616438, 25.2684931506849,
5.25479452054795, 6.33698630136986, 26.3506849315068, 2.87671232876712,
4.29315068493151, 2.78630136986301, 1.2958904109589, 1.75890410958904,
2.29041095890411, 7.2958904109589, 3.44657534246575, 4.5041095890411,
9.73424657534247, 8.21643835616438, 6.29315068493151, 7.50684931506849,
3.75616438356164, 7.96438356164384, 9.96438356164384, 29.9780821917808,
17.3452054794521, 26.8547945205479, 5.5041095890411, 7.17260273972603,
6.13150684931507, 3.62739726027397, 5.79452054794521, 7.7972602739726,
5.75616438356164, 3.09041095890411, 8.0958904109589, 9.63013698630137,
4.93424657534247, 6.93424657534247, 9.93698630136986, 19.9452054794521,
6.42191780821918, 8.96438356164384, 28.9780821917808, 15.7643835616438,
18.7205479452055, 1.20547945205479, 4.16986301369863, 24.1835616438356,
25.2520547945205, 26.3095890410959, 9.61917808219178, 12.3424657534247,
14.2986301369863, 23.7698630136986, 3.71232876712329, 8.71506849315069,
3.58082191780822, 1.25205479452055, 5.96164383561644, 3.46027397260274,
8.46301369863014, 7.88219178082192, 5.54246575342466, 6.46027397260274,
4.79452054794521, 3.20821917808219, 15.5506849315068, 9.5972602739726,
20.6, 2.09041095890411, 1.87671232876712, 22.2191780821918, 5.67397260273973,
25.7260273972603, 26.9753424657534, 7.2958904109589, 27.5205479452055,
8.17260273972603, 28.3095890410959, 29.2246575342466, 3.87945205479452,
9.13424657534247, 5.84109589041096, 3.96164383561644, 4.54246575342466,
9.92602739726027, 29.9397260273973, 16.1397260273973, 16.1780821917808,
16.1397260273973, 18.7643835616438, 18.7643835616438, 20.2191780821918,
20.6383561643836, 20.8904109589041, 1.16712328767123, 21.2657534246575,
1.33424657534247, 2.04383561643836, 2.20547945205479, 4.75616438356164,
2.92054794520548, 23.2219178082192, 4.09315068493151, 4.24931506849315,
5.13150684931507, 5.96164383561644, 6.20821917808219, 27.3917808219178,
29.2246575342466, 9.75890410958904, 8.75890410958904, 28.2712328767123,
8.42465753424658, 28.4383561643836, 2.37260273972603, 4.37534246575343,
4.75616438356164, 1.67123287671233, 3.96164383561644, 2.37260273972603,
5.04657534246575, 2.25205479452055, 5.62739726027397, 4.29315068493151,
1.29041095890411, 3.5041095890411, 5.5041095890411, 5.09315068493151,
3.83561643835616, 8.46301369863014, 3.42191780821918, 1.33424657534247,
4.5041095890411, 4.79452054794521, 6.37534246575343, 7.21095890410959,
9.37808219178082, 29.3534246575342, 3.54246575342466, 28.6904109589041,
8.2958904109589, 8.54246575342466, 4.62739726027397, 12.4082191780822,
19.4712328767123, 20.972602739726, 3.67397260273973, 4.42191780821918,
4.46027397260274, 25.3890410958904, 5.87945205479452, 25.8931506849315,
2.20547945205479, 27.2246575342466, 7.88219178082192, 1.43561643835616,
2.43561643835616, 4.46027397260274, 6.46027397260274, 9.46301369863014,
19.4712328767123, 29.4767123287671, 2.45753424657534, 9.63013698630137,
7.88219178082192, 23.641095890411, 5.13150684931507, 6.46027397260274,
9.7972602739726, 29.8109589041096, 3.21095890410959, 8.21643835616438,
4.92328767123288, 3.25479452054795, 2.62465753424658, 3.71232876712329,
5.75616438356164, 25.7698630136986, 6.20821917808219, 1.20547945205479,
4.96164383561644, 2.09041095890411, 4.13150684931507, 2.45753424657534,
27.4767123287671, 7.46301369863014, 3.2, 18.2164383561644, 1.1972602739726,
5.20821917808219, 8.21095890410959, 28.2246575342466, 19.972602739726,
7.50684931506849, 9.92602739726027, 3.75616438356164, 1.38904109589041,
3.39178082191781, 19.4082191780822, 2.04383561643836, 24.0602739726027,
5.75616438356164, 26.4356164383562, 6.42191780821918, 28.0630136986301,
8.21095890410959, 18.2164383561644, 6.37534246575343, 3.96164383561644,
6.87945205479452, 7.75890410958904, 5.54246575342466, 7.75890410958904,
2.19452054794521, 27.213698630137, 29.227397260274, 8.2, 5.87945205479452,
1.12876712328767, 3.12876712328767, 7.54520547945206, 2.74246575342466,
4.74520547945205, 6.74520547945205, 9.74794520547945, 29.7616438356164,
3.58904109589041, 8.53150684931507, 18.5369863013699, 28.5452054794521,
4.96164383561644, 19.3479452054795, 1.04657534246575, 21.386301369863,
3.96164383561644, 5.42191780821918, 3.62739726027397, 7.13150684931507,
2.54246575342466, 27.5616438356164, 18.0547945205479, 4.16986301369863,
9.25753424657534, 4.76986301369863, 15.2164383561644, 6.37534246575343,
8.17808219178082, 3.73424657534247, 1.26849315068493, 6.27123287671233,
6.2986301369863, 19.3945205479452, 17.1397260273973, 1.20547945205479,
6.20821917808219, 3.37534246575342, 5.25479452054795, 4.75616438356164,
6.54246575342466, 3.16986301369863, 2.04383561643836, 4.25479452054795,
1.04383561643836, 7.71506849315069, 5.09315068493151, 4.42191780821918,
6.71232876712329, 3.04383561643836, 1.67945205479452, 3.30958904109589,
3.96164383561644, 2.92054794520548, 7.92602739726027, 27.9397260273973,
2.83835616438356, 7.46301369863014, 8.21095890410959, 7.23835616438356,
27.2520547945205, 2.53972602739726, 6.58904109589041, 9.96438356164384,
29.9780821917808, 16.4301369863014, 18.3671232876712, 19.2657534246575,
20.386301369863, 2.37534246575342, 6.46027397260274, 26.4739726027397,
6.62739726027397, 4.75616438356164, 6.71232876712329, 28.8958904109589,
15.5123287671233, 14.8465753424658, 14.8465753424658, 2.0027397260274,
3.20821917808219, 3.71232876712329, 1.6958904109589, 2.07671232876712,
7.75616438356164, 2.88493150684931, 4.29315068493151, 1.84931506849315,
8.8958904109589, 1.12328767123288, 5.38630136986301, 5.12602739726027,
8.25205479452055, 3.24931506849315, 1.38904109589041, 5.89315068493151,
6.60821917808219, 1.5013698630137, 3.46027397260274, 6.5041095890411,
7.75890410958904, 6.54246575342466, 9.37808219178082, 3.96164383561644,
5.04657534246575, 7.04657534246575, 8.13424657534247, 6.13150684931507,
9.37808219178082, 7.96438356164384, 11.0493150684932, 12.213698630137,
2.12876712328767, 6.67397260273973, 4.46027397260274, 24.4739726027397,
3.75616438356164, 8.75890410958904, 18.7643835616438, 8.46301369863014,
5.13150684931507, 9.88219178082192, 9.33150684931507, 5.96164383561644,
7.25753424657534, 5.62739726027397, 7.50684931506849, 4.84109589041096,
3.46027397260274, 3.92054794520548, 18.5095890410959, 7.13150684931507,
8.59178082191781, 3.42191780821918, 6.75616438356164, 4.75616438356164,
2.95890410958904, 3.96164383561644, 20.4328767123288, 1.41917808219178,
21.4328767123288, 22.1041095890411, 2.09041095890411, 23.2219178082192,
3.20821917808219, 3.75616438356164, 24.7698630136986, 25.4739726027397,
4.46027397260274, 2.75970346198464, 3.45063169924232, 3.64711503029937,
4.21614538082358, 4.46897200885451, 4.78330432863798, 2.44945159376164,
3.0246987676982, 3.18637219409113, 2.82704067767991, 3.39633877759394,
4.01211492772477, 3.58227507536714, 3.26211339894416, 4.58021400951056,
3.24894671597498, 3.81640051029768, 4.29811641034397, 2.41235239871336,
4.16381972504066, 4.82320604897032, 2.85924829032196, 3.60102245001335,
4.38947242891732, 4.83225858506487, 2.32276057802484, 2.77466777269801,
3.53613679638981, 4.11732178923756, 4.70239811454869, 5.02684596680901,
4.89394362719873, 2.95858956427959, 3.67972641713649, 4.76329504168897,
4.91952178329268, 3.16243766754998, 4.53553009720828, 3.25401782240246,
4.57975033696973, 3.83648225102819, 4.85066377882221, 3.90946630290976,
4.85924427553214, 3.7665702648057, 3.63632959242775, 3.90561093988307,
4.82873462476434, 4.25993172383506, 3.47661513525158, 2.08147939877802,
3.04373161945452, 3.60670989847648, 4.16882946922235, 4.45461902145002,
4.34162315280691, 4.48802758727908, 1.86206866940942, 4.01581962612883,
2.69771901556242, 3.47661513525158, 4.29494911958988, 2.50903075277185,
3.20217588142397, 3.83011128813872, 3.980829920259, 4.28800748360142,
3.58227507536714, 4.30653857989399, 4.82319396445967, 4.50784277427558,
1.94072216406505, 1.96202863703256, 3.25200612691594, 4.52865722521467,
4.48533445537866, 4.26859229653523, 4.53731981080683, 4.27200767176305,
4.26457120140531, 4.2589693893104, 4.53525135174211, 2.21846989417729,
4.45880330075475, 4.40569121171677, 1.7068990376281, 2.57392106736465,
4.31836655689863, 2.66826592342152, 2.96442641830962, 2.16545787147435,
4.35324929683238, 4.32386522416303, 2.50903075277185, 3.39298854017977,
4.26232802572813, 1.70955300282675, 3.52741837222827, 4.25907996756536,
2.18557386001823, 3.77060768900683, 4.26395740412398, 2.18557386001823,
2.95157192131744, 3.53780855712753, 4.12926524629975, 4.46454130003024,
4.32641578537161, 2.63737387449928, 2.45327077112083, 3.28325793775465,
4.41181850146463, 3.01267661647382, 4.30273228804522, 3.0809392572752,
3.77893332429253, 4.80391883502991, 4.29751273896985, 4.9988952974639,
4.94101980513708, 4.96053762255352, 2.81894331471379, 4.91003396772332,
3.27522101695249, 4.87134979305318, 4.55194984744145, 3.20084942592193,
3.86347792664764, 5.03902446306499, 5.00360541053749, 4.93013307063464,
2.81894331471379, 4.89111202391337, 4.84484889948005, 3.62226986596664,
4.83000471370643, 3.87684000847915, 4.16249148956975, 4.82319682171546,
3.07342513615359, 3.58227507536714, 3.03776390795916, 2.39174353424389,
2.60441382154721, 2.83511983796416, 4.3776430683761, 3.28927099619789,
3.65030540624117, 4.78053976420514, 3.98851220598087, 3.84027419541323,
4.10888701756863, 3.08864056496118, 4.50784277427558, 4.80886282388512,
4.94393108371142, 5.0798798303101, 4.82563811883379, 3.9468402869224,
4.35189109140176, 4.11184051977588, 3.35459034230467, 4.02517710657812,
4.47676297113471, 4.01502608262676, 2.59176865382458, 3.96722952163555,
4.76721313996148, 3.21864402185455, 3.73613392685585, 4.2411779069777,
4.43398643591996, 3.61855452792053, 4.10980345553821, 4.31985435471796,
5.10135192405871, 5.04171773535286, 2.34892141444876, 3.5416097666887,
4.85054794533227, 4.83022204838818, 4.82316539945316, 4.76579152488111,
4.70585851301182, 4.77956222001954, 4.54973985879503, 3.38475186086735,
4.63579216766113, 3.33790973940492, 2.37103458556849, 3.75717008349914,
2.98272273951349, 4.28337045807013, 4.49267828838454, 3.9573829235615,
4.19209673281346, 3.74079199043232, 3.20084942592193, 5.10187826143602,
4.76293750094991, 4.97336905684238, 2.74996824339996, 2.65677488819793,
4.91172311677223, 3.99307443262087, 4.82526350378577, 4.8268073878022,
4.3776430683761, 4.83510606972691, 4.54522430364073, 4.85652654574101,
4.89686171640296, 2.87874743905149, 4.13483923719495, 4.03742401175551,
3.47139417367879, 3.66246558484484, 4.80424996332984, 4.94121262270349,
4.53454492449136, 4.53419598722523, 4.53454492449136, 4.47589167424468,
4.47589167424468, 4.42360799906947, 4.4074923871952, 4.39773506579433,
2.01907578318878, 4.38322128535129, 1.84538737297096, 2.41830268718988,
2.23480800909955, 3.16465475329972, 2.52618004042805, 4.56596058005526,
2.95157192131744, 3.00349899341948, 3.52972300068793, 3.75717008349914,
3.56654832534446, 4.26829419274457, 4.33246733302122, 4.47209933166968,
4.07829496689505, 4.54366179335202, 4.02412309850527, 4.29676089988903,
2.8695390825768, 3.60901246346208, 3.72904913668145, 2.56498963399193,
3.47139417367879, 2.8695390825768, 3.81640051029768, 2.81894331471379,
3.98051398879079, 3.58227507536714, 2.38916039360867, 3.31021811457649,
3.9468402869224, 3.83007753533476, 3.42794910435245, 4.59492002573633,
3.28024670656985, 2.4097817563527, 3.65030540624117, 3.74079199043232,
4.17176477555296, 4.35996196359352, 4.73359981456944, 4.90398228783007,
3.32409784536479, 4.87118581003356, 4.56666593488235, 4.60802805678997,
3.68916442604293, 5.02118529619339, 4.4514690518147, 4.39455164410878,
2.80677702722226, 3.05963754502245, 3.07193520904601, 4.2641136733005,
3.48305015568073, 4.25979660315916, 2.23480800909955, 4.26558395648925,
3.9282839050028, 1.89269207014021, 2.33130752327983, 3.07193520904601,
3.62770234943172, 4.18074663670204, 4.4514690518147, 4.34675554764834,
2.34036194508005, 4.20281875657975, 4.49267828838454, 4.86491005913173,
3.84127256835413, 4.19209673281346, 4.78844960315538, 4.93234475967529,
3.20188086629451, 4.5529065893626, 3.7797549774533, 3.21833597902597,
2.97302436839012, 3.38475186086735, 4.01502608262676, 4.82494685548552,
4.1309427087262, 2.34892141444876, 3.79122469981557, 2.74996824339996,
3.52882080169136, 2.90475632846179, 4.83425050183613, 4.41167553240461,
3.19775298791517, 5.05733248819791, 2.34500731502677, 3.86347792664764,
4.55194984744145, 4.8536532162969, 4.68580090262939, 3.8560422018531,
4.80424996332984, 2.83579574924565, 2.43541801827223, 3.26917867887851,
4.70658092262176, 2.41830268718988, 4.54205071405147, 3.70347651496055,
4.51179026913548, 3.87139934363606, 4.53702297816656, 4.24040027977525,
4.74578292053171, 4.17176477555296, 3.15984460601259, 3.9768746297032,
4.46949323821673, 3.9573829235615, 4.46949323821673, 2.79454182212808,
4.82981722014996, 4.89700936635987, 4.55003332824908, 4.04744453906246,
2.31225215646125, 3.17078397447093, 4.42804643606615, 3.02033214617563,
3.72568245460231, 4.25829574912718, 4.78226928394965, 4.92905316482749,
3.34086061969711, 4.04183801378363, 4.48319605565641, 4.30085456068988,
3.79122469981557, 4.45589006029623, 1.70822621819291, 4.63142865759546,
2.90699979029706, 3.35965176254463, 2.79019595892293, 3.77878982474076,
2.37522576957373, 4.27154459842833, 4.49758018625893, 2.97721538330697,
4.15244511731778, 3.73325024745895, 5.10139379386631, 4.17176477555296,
4.54618813621284, 3.39248194962301, 2.37881218415977, 4.1464619591812,
4.15316130101696, 4.70707172705424, 4.77267338855031, 2.03737184678255,
3.81939314106, 3.26312377270195, 3.87684000847915, 3.72904913668145,
4.21150941368065, 2.87482262642492, 2.41830268718988, 3.25814616479328,
1.95974385334363, 4.14957253152334, 3.51852796766856, 3.31248236073798,
3.93926475863266, 2.82676588308018, 2.25715273670682, 3.23877823069724,
3.47139417367879, 3.09057442380978, 4.50079498118852, 4.84503359841119,
3.05834404390917, 4.41167553240461, 4.55194984744145, 4.36569411888241,
4.83039001292176, 2.93850101323729, 4.22239549435516, 4.80886282388512,
4.94393108371142, 5.09584661299229, 5.05283820324407, 5.02319768413649,
4.67005370266932, 2.87068065591085, 4.19209673281346, 4.82343926231588,
3.91974901504793, 3.72904913668145, 4.25081432629887, 4.88033737221285,
4.53751058675557, 5.09889228357165, 5.09889228357165, 2.7120126164778,
3.20084942592193, 3.38475186086735, 2.57611729759449, 2.74406304100437,
4.46897200885461, 3.07664762906049, 3.58227507536714, 2.64466064962826,
4.66384481684621, 2.30962110926233, 3.91408252149265, 3.83967705403174,
4.55910078340349, 3.21628402040912, 2.43541801827223, 4.05100906700729,
4.22685401869853, 2.48748627144419, 3.29427230717969, 4.20248230878606,
4.46949323821673, 4.21150941368065, 4.73359981456944, 3.47139417367879,
3.81640051029768, 4.32499408591158, 4.53844903952499, 4.11184051977588,
4.73359981456944, 4.50784277427558, 4.92221662489616, 5.00972596707877,
2.76645294455757, 4.24203451863899, 3.63632959242775, 4.8438946910502,
3.40019013262738, 4.64268935027678, 5.04028605762641, 4.59492002573633,
3.84127256835413, 4.79892563273548, 4.41562762186783, 4.06871965116534,
4.36969042097439, 3.98051398879079, 4.42043658523483, 3.75496657158094,
3.29427230717969, 3.45730608585978, 5.04844935470778, 3.77878982474076,
4.05166542652766, 3.28024670656985, 4.26078052262918, 3.72904913668145,
3.10550475341744, 3.47139417367879, 4.41541846233588, 1.88505721037991,
4.3767970882106, 4.35153448481756, 2.18557386001823, 4.31311576433973,
2.6364550425402, 2.83579574924565, 4.27355104887647, 4.26316357649486,
3.07193520904601), .Dim = c(500L, 2L), .Dimnames = list(c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
"14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24",
"25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35",
"36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46",
"47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57",
"58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68",
"69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79",
"80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90",
"91", "92", "93", "94", "95", "96", "97", "98", "99", "100",
"101", "102", "103", "104", "105", "106", "107", "108", "109",
"110", "111", "112", "113", "114", "115", "116", "117", "118",
"119", "120", "121", "122", "123", "124", "125", "126", "127",
"128", "129", "130", "131", "132", "133", "134", "135", "136",
"137", "138", "139", "140", "141", "142", "143", "144", "145",
"146", "147", "148", "149", "150", "151", "152", "153", "154",
"155", "156", "157", "158", "159", "160", "161", "162", "163",
"164", "165", "166", "167", "168", "169", "170", "171", "172",
"173", "174", "175", "176", "177", "178", "179", "180", "181",
"182", "183", "184", "185", "186", "187", "188", "189", "190",
"191", "192", "193", "194", "195", "196", "197", "198", "199",
"200", "201", "202", "203", "204", "205", "206", "207", "208",
"209", "210", "211", "212", "213", "214", "215", "216", "217",
"218", "219", "220", "221", "222", "223", "224", "225", "226",
"227", "228", "229", "230", "231", "232", "233", "234", "235",
"236", "237", "238", "239", "240", "241", "242", "243", "244",
"245", "246", "247", "248", "249", "250", "251", "252", "253",
"254", "255", "256", "257", "258", "259", "260", "261", "262",
"263", "264", "265", "266", "267", "268", "269", "270", "271",
"272", "273", "274", "275", "276", "277", "278", "279", "280",
"281", "282", "283", "284", "285", "286", "287", "288", "289",
"290", "291", "292", "293", "294", "295", "296", "297", "298",
"299", "300", "301", "302", "303", "304", "305", "306", "307",
"308", "309", "310", "311", "312", "313", "314", "315", "316",
"317", "318", "319", "320", "321", "322", "323", "324", "325",
"326", "327", "328", "329", "330", "331", "332", "333", "334",
"335", "336", "337", "338", "339", "340", "341", "342", "343",
"344", "345", "346", "347", "348", "349", "350", "351", "352",
"353", "354", "355", "356", "357", "358", "359", "360", "361",
"362", "363", "364", "365", "366", "367", "368", "369", "370",
"371", "372", "373", "374", "375", "376", "377", "378", "379",
"380", "381", "382", "383", "384", "385", "386", "387", "388",
"389", "390", "391", "392", "393", "394", "395", "396", "397",
"398", "399", "400", "401", "402", "403", "404", "405", "406",
"407", "408", "409", "410", "411", "412", "413", "414", "415",
"416", "417", "418", "419", "420", "421", "422", "423", "424",
"425", "426", "427", "428", "429", "430", "431", "432", "433",
"434", "435", "436", "437", "438", "439", "440", "441", "442",
"443", "444", "445", "446", "447", "448", "449", "450", "451",
"452", "453", "454", "455", "456", "457", "458", "459", "460",
"461", "462", "463", "464", "465", "466", "467", "468", "469",
"470", "471", "472", "473", "474", "475", "476", "477", "478",
"479", "480", "481", "482", "483", "484", "485", "486", "487",
"488", "489", "490", "491", "492", "493", "494", "495", "496",
"497", "498", "499", "500"), NULL))


问:
How to pass a Bundle to Android Wear using MessageApi
I'm currently passing Bytes to AnroidWear using:

```<code> MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(
                        mGoogleApiClient, node.getId(), path, text.getBytes() ).await();
</code>```

I want to send a proper Data Bundle to my Wearable, how do I do that?

答:
Convert your bundle to bytes, and on receiver, convert bytes to Bundle.

With this simple method, you can send only String Bundle values.

(And, It seems that you can convert Bundle to String with GSON and BundleTypeAdapterFactory also, but I'm not tested.)

```<code>public void example() {
    // Value
    Bundle inBundle = new Bundle();
    inBundle.putString("key1", "value");
    inBundle.putInt("key2", 1); // will be failed
    inBundle.putFloat("key3", 0.5f); // will be failed
    inBundle.putString("key4", "this is key4");

    // From Bundle to bytes
    byte[] inBytes = bundleToBytes(inBundle);

    // From bytes to Bundle
    Bundle outBundle = jsonStringToBundle(new String(inBytes));

    // Check values
    String value1 = outBundle.getString("key1"); // good
    int value2 = outBundle.getInt("key2"); // fail
    float value3 = outBundle.getFloat("key3"); // fail
    String value4 = outBundle.getString("key4"); // good

}

private byte[] bundleToBytes(Bundle inBundle) {
    JSONObject json = new JSONObject();
    Set<String> keys = inBundle.keySet();
    for (String key : keys) {
        try {
            json.put(key, inBundle.get(key));
        } catch (JSONException e) {
            //Handle exception here
        }
    }

    return json.toString().getBytes();
}

public static Bundle jsonStringToBundle(String jsonString) {
    try {
        JSONObject jsonObject = new JSONObject(jsonString);
        return jsonToBundle(jsonObject);
    } catch (JSONException ignored) {

    }
    return null;
}

public static Bundle jsonToBundle(JSONObject jsonObject) throws JSONException {
    Bundle bundle = new Bundle();
    Iterator iter = jsonObject.keys();
    while (iter.hasNext()) {
        String key = (String) iter.next();
        String value = jsonObject.getString(key);
        bundle.putString(key, value);
    }
    return bundle;
}
</code>```

Body:
I'm currently passing Bytes to AnroidWear using:

 MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(
                        mGoogleApiClient, node.getId(), path, text.getBytes() ).await();

I want to send a proper Data Bundle to my Wearable, how do I do that?

Body:
Convert your bundle to bytes, and on receiver, convert bytes to Bundle.

With this simple method, you can send only String Bundle values.

(And, It seems that you can convert Bundle to String with GSON and BundleTypeAdapterFactory also, but I'm not tested.)

public void example() {
    // Value
    Bundle inBundle = new Bundle();
    inBundle.putString("key1", "value");
    inBundle.putInt("key2", 1); // will be failed
    inBundle.putFloat("key3", 0.5f); // will be failed
    inBundle.putString("key4", "this is key4");

    // From Bundle to bytes
    byte[] inBytes = bundleToBytes(inBundle);

    // From bytes to Bundle
    Bundle outBundle = jsonStringToBundle(new String(inBytes));

    // Check values
    String value1 = outBundle.getString("key1"); // good
    int value2 = outBundle.getInt("key2"); // fail
    float value3 = outBundle.getFloat("key3"); // fail
    String value4 = outBundle.getString("key4"); // good

}

private byte[] bundleToBytes(Bundle inBundle) {
    JSONObject json = new JSONObject();
    Set<String> keys = inBundle.keySet();
    for (String key : keys) {
        try {
            json.put(key, inBundle.get(key));
        } catch (JSONException e) {
            //Handle exception here
        }
    }

    return json.toString().getBytes();
}

public static Bundle jsonStringToBundle(String jsonString) {
    try {
        JSONObject jsonObject = new JSONObject(jsonString);
        return jsonToBundle(jsonObject);
    } catch (JSONException ignored) {

    }
    return null;
}

public static Bundle jsonToBundle(JSONObject jsonObject) throws JSONException {
    Bundle bundle = new Bundle();
    Iterator iter = jsonObject.keys();
    while (iter.hasNext()) {
        String key = (String) iter.next();
        String value = jsonObject.getString(key);
        bundle.putString(key, value);
    }
    return bundle;
}