春季启动:内部异常:com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:连接关闭后不允许进行任何操作

时间:2018-06-28 09:51:16

标签: mysql spring spring-boot jpa jdbc

我在应用程序中配置了两个数据源,每个数据源都连接到不同的数据库。

    @Configuration
@EnableJpaRepositories(basePackages = "com.bancadati.*",
        entityManagerFactoryRef = "entityManagerFactory", transactionManagerRef = "transactionManager")
public class BancaDatiDbConfig {

    @Bean(name = "dataSource")
    @Primary
    public DataSource dataSource() {
        try {
            final Properties pool = new Properties();
            pool.put("driverClassName", "com.mysql.jdbc.Driver");
            pool.put("url", "jdbc:mysql://localhost:3306/bd");
            pool.put("username", "xxx");
            pool.put("password", "xxx");
            pool.put("minIdle", 1);
            pool.put("maxWait", 10000);
            pool.put("testOnBorrow", true);
            pool.put("validationQuery", "SELECT 1");
            pool.put("autoReconnect", true);
            return new org.apache.tomcat.jdbc.pool.DataSourceFactory().createDataSource(pool);
        }catch (Exception e){

            throw new RuntimeException("can't create mysql bancadati pool.");
        }
    }

    @PersistenceContext(unitName = "bancadati")
    @Primary
    @Bean(name = "entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder) {
        return builder.dataSource(dataSource())
                .persistenceUnit("bancadati")
                .packages("com.bancadati.*")
                .build();
    }

    @Primary
    @Bean(name = "transactionManager")
    public PlatformTransactionManager transactionManager(EntityManagerFactoryBuilder builder) {
        JpaTransactionManager tm = new JpaTransactionManager();
        tm.setEntityManagerFactory(entityManagerFactory(builder).getObject());
        tm.setDataSource(dataSource());
        return tm;
    }

}

和第二个。

@Configuration
@EnableJpaRepositories(
        entityManagerFactoryRef = "fallmentiNewsEntityManager",
        transactionManagerRef = "fallimentiNewsTransactionManager",
        basePackages = "com.fallimentinews.*")
public class FallimentiNewsDbConfig {

    @Bean(name = "fallimentiNewsDataSource")
    public DataSource flnewsDataSource() {
        try {
            final Properties pool = new Properties();
            pool.put("driverClassName", "com.mysql.jdbc.Driver");
            pool.put("url", "jdbc:mysql://192.168.111.199:3306/flnews");
            pool.put("username", "xxx");
            pool.put("password", "xxx");
            pool.put("minIdle", 1);
            pool.put("testOnBorrow", true);
            return new org.apache.tomcat.jdbc.pool.DataSourceFactory().createDataSource(pool);
        }catch (Exception e){
            throw new RuntimeException("Impossibile creare mysql pool per fallimentinews.");
        }
    }

    @PersistenceContext(unitName = "fallimentinews")
    @Bean(name = "fallmentiNewsEntityManager")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder) {
        return  builder.dataSource(flnewsDataSource())
                .persistenceUnit("fallimentinews")
                .packages("com.fallimentinews.*")
                .build();
    }

    @Bean(name = "fallimentiNewsTransactionManager")
    public PlatformTransactionManager transactionManager(EntityManagerFactoryBuilder builder) {
        JpaTransactionManager tm = new JpaTransactionManager();
        tm.setEntityManagerFactory(entityManagerFactory(builder).getObject());
        tm.setDataSource(flnewsDataSource());
        return tm;
    }
}

它是随机发生的,由于某种原因,mysql连接关闭并且应用程序无法再重新连接到db,从而收到此错误:

[EL Finer]: connection: ServerSession(663238966)--client acquired: 894375198
[EL Finer]: transaction: ClientSession(894375198)--acquire unit of work: 120676294
[EL Finest]: connection: ServerSession(663238966)--Connection(940365957)--Connection acquired from connection pool [read].
[EL Finest]: connection: ServerSession(663238966)--reconnecting to external connection pool
[EL Fine]: sql: ServerSession(663238966)--Connection(267520764)--SELECT id, label, slug, view, visible, price_list_id FROM action WHERE (slug = ?)
    bind => [add-task]
[EL Fine]: sql: ServerSession(663238966)--SELECT 1
[EL Finest]: connection: ServerSession(663238966)--Connection(940365957)--Connection released to connection pool [read].
[EL Info]: query: UnitOfWork(120676294)--Communication failure detected when attempting to perform read query outside of a transaction. Attempting to retry query. Error was: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.7.1.v20171221-bd47e8f): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
Error Code: 0

为什么应用程序无法重新连接到数据库?有任何想法吗?我可以重现停止和重新启动mysql服务的问题。 谢谢。

在堆栈跟踪下方:

Call: SELECT id, label, slug, view, visible, price_list_id FROM action WHERE (slug = ?)
    bind => [add-task]
Query: ReadAllQuery(referenceClass=Action sql="SELECT id, label, slug, view, visible, price_list_id FROM action WHERE (slug = ?)")
    at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:340)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.processExceptionForCommError(DatabaseAccessor.java:1645)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:680)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeCall(DatabaseAccessor.java:564)
    at org.eclipse.persistence.internal.sessions.AbstractSession.basicExecuteCall(AbstractSession.java:2093)
    at org.eclipse.persistence.sessions.server.ServerSession.executeCall(ServerSession.java:600)
    at org.eclipse.persistence.sessions.server.ClientSession.executeCall(ClientSession.java:261)
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:270)
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:256)
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeSelectCall(DatasourceCallQueryMechanism.java:327)
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.selectAllRows(DatasourceCallQueryMechanism.java:722)
    at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.selectAllRowsFromTable(ExpressionQueryMechanism.java:2740)
    at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.selectAllRows(ExpressionQueryMechanism.java:2693)
    at org.eclipse.persistence.queries.ReadAllQuery.executeObjectLevelReadQuery(ReadAllQuery.java:563)
    at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeDatabaseQuery(ObjectLevelReadQuery.java:1221)
    at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:911)
    at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1180)
    at org.eclipse.persistence.queries.ReadAllQuery.execute(ReadAllQuery.java:464)
    at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork(ObjectLevelReadQuery.java:1268)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2979)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1892)
    at org.eclipse.persistence.internal.sessions.AbstractSession.retryQuery(AbstractSession.java:1963)
    at org.eclipse.persistence.sessions.server.ClientSession.retryQuery(ClientSession.java:702)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.retryQuery(UnitOfWorkImpl.java:5660)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1929)
    at org.eclipse.persistence.internal.sessions.AbstractSession.retryQuery(AbstractSession.java:1963)
    at org.eclipse.persistence.sessions.server.ClientSession.retryQuery(ClientSession.java:702)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.retryQuery(UnitOfWorkImpl.java:5660)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1929)
    at org.eclipse.persistence.internal.sessions.AbstractSession.retryQuery(AbstractSession.java:1963)
    at org.eclipse.persistence.sessions.server.ClientSession.retryQuery(ClientSession.java:702)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.retryQuery(UnitOfWorkImpl.java:5660)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1929)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1874)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1839)
    at org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(QueryImpl.java:260)
    ... 45 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
    at sun.reflect.GeneratedConstructorAccessor904.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
    at com.mysql.jdbc.Util.getInstance(Util.java:408)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:918)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
    at com.mysql.jdbc.ConnectionImpl.throwConnectionClosedException(ConnectionImpl.java:1187)
    at com.mysql.jdbc.ConnectionImpl.checkClosed(ConnectionImpl.java:1182)
    at com.mysql.jdbc.ConnectionImpl.prepareStatement(ConnectionImpl.java:4071)
    at com.mysql.jdbc.ConnectionImpl.prepareStatement(ConnectionImpl.java:4040)
    at sun.reflect.GeneratedMethodAccessor148.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.tomcat.jdbc.pool.ProxyConnection.invoke(ProxyConnection.java:126)
    at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:108)
    at org.apache.tomcat.jdbc.pool.interceptor.AbstractCreateStatementInterceptor.invoke(AbstractCreateStatementInterceptor.java:75)
    at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:108)
    at org.apache.tomcat.jdbc.pool.DisposableConnectionFacade.invoke(DisposableConnectionFacade.java:81)
    at com.sun.proxy.$Proxy1506.prepareStatement(Unknown Source)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.prepareStatement(DatabaseAccessor.java:1590)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.prepareStatement(DatabaseAccessor.java:1539)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseCall.prepareStatement(DatabaseCall.java:804)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:625)
    ... 78 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Server shutdown in progress
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
    at com.mysql.jdbc.Util.getInstance(Util.java:408)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:918)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2487)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858)
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1966)
    at sun.reflect.GeneratedMethodAccessor149.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(StatementFacade.java:114)
    at com.sun.proxy.$Proxy1569.executeQuery(Unknown Source)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeSelect(DatabaseAccessor.java:1012)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:648)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeCall(DatabaseAccessor.java:564)
    at org.eclipse.persistence.internal.sessions.AbstractSession.basicExecuteCall(AbstractSession.java:2093)
    at org.eclipse.persistence.sessions.server.ServerSession.executeCall(ServerSession.java:600)
    at org.eclipse.persistence.sessions.server.ClientSession.executeCall(ClientSession.java:261)
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:270)
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:256)
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeSelectCall(DatasourceCallQueryMechanism.java:327)
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.selectAllRows(DatasourceCallQueryMechanism.java:722)
    at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.selectAllRowsFromTable(ExpressionQueryMechanism.java:2740)
    at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.selectAllRows(ExpressionQueryMechanism.java:2693)
    at org.eclipse.persistence.queries.ReadAllQuery.executeObjectLevelReadQuery(ReadAllQuery.java:563)
    at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeDatabaseQuery(ObjectLevelReadQuery.java:1221)
    at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:911)
    at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1180)
    at org.eclipse.persistence.queries.ReadAllQuery.execute(ReadAllQuery.java:464)
    at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork(ObjectLevelReadQuery.java:1268)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2979)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1892)
    ... 48 more

0 个答案:

没有答案