2
回答
事务提交异常com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Communications link failure during commit()
开发十年,就只剩下这套Java开发体系了   

@JFinal 你好,想跟你请教个问题:

我使用Db.tx来进行事务提交,但是报异常

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Communications link failure during commit(). Transaction resolution unknown.

可能是什么原因呢?

代码如下:

Db.tx(new IAtom() {
    @Override
    public boolean run() throws SQLException {
        return saveWithDbName(Constant.DATA_BASE.MAIN_DB);
    }
});

 

public boolean saveWithDbName(String mysqlName){
    logger.info("mysqlName : " + mysqlName);
    boolean result;
    try{
        if(sqlMap.containsKey("sql_website")){
            String sql_website = sqlMap.get("sql_website");
            //count_class = Db.use(mysqlName).update(sql_website);
            logger.info("开始获取连接");
            count_class = (int)Db.use(mysqlName).execute((conn) -> {
                logger.info("开始获取PreparedStatement");
                PreparedStatement pst = conn.prepareStatement(sql_website);
                logger.info("开始执行");
                int ret = pst.executeUpdate();
                logger.info("开始关闭");
                if (pst != null) {try {pst.close();} catch (SQLException e) {throw new ActiveRecordException(e);}}
                logger.info("关闭完成");
                return ret;
            });
        }
        if(sqlMap.containsKey("sql_org_first_insert")){
            String sql_org_first_insert = sqlMap.get("sql_org_first_insert");
            Db.use(mysqlName).update(sql_org_first_insert);
        }
        if(sqlMap.containsKey("sql_org_first_update")){
            String sql_org_first_update = sqlMap.get("sql_org_first_update");
            count_level1 =  Db.use(mysqlName).update(sql_org_first_update);
        }
        if(sqlMap.containsKey("sql_org_second_insert")){
            String sql_org_second_insert = sqlMap.get("sql_org_second_insert");
            Db.use(mysqlName).update(sql_org_second_insert);
        }
        if(sqlMap.containsKey("sql_org_second_update")){
            String sql_org_second_update = sqlMap.get("sql_org_second_update");
            count_level2 = Db.use(mysqlName).update(sql_org_second_update);
        }
        if(sqlMap.containsKey("sql_org_third_insert")){
            String sql_website = sqlMap.get("sql_org_third_insert");
            Db.use(mysqlName).update(sql_website);
        }
        if(sqlMap.containsKey("sql_org_third_update")){
            String sql_website = sqlMap.get("sql_org_third_update");
            count_level3 = Db.use(mysqlName).update(sql_website);
        }
        result = true;
    }catch(Exception e){
        logger.error("机构同步保存数据库发生异常,mysqlName=="+mysqlName+",错误信息:"+e.getMessage());
        e.printStackTrace();
        result = false;
    }
    return result;
}

 

异常如下:

com.jfinal.plugin.activerecord.ActiveRecordException: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Communications link failure during commit(). Transaction resolution unknown.
    at com.jfinal.plugin.activerecord.DbPro.tx(DbPro.java:723)
    at com.jfinal.plugin.activerecord.DbPro.tx(DbPro.java:748)
    at com.jfinal.plugin.activerecord.Db.tx(Db.java:460)
    at cn.esstx.dzg.runner.tinyrunner.model.Organization.saveWithReportDbTx(Organization.java:89)
    at cn.esstx.dzg.runner.tinyrunner.service.ds.org.OrganizationService.importData(OrganizationService.java:145)
    at cn.esstx.dzg.runner.tinyrunner.service.ds.org.OrgDataSyncService.import2Database(OrgDataSyncService.java:118)
    at cn.esstx.dzg.runner.tinyrunner.service.ds.org.OrgDataSyncService.importData(OrgDataSyncService.java:46)
    at cn.esstx.dzg.runner.tinyrunner.job.OrganizationImportJob.execute_aroundBody0(OrganizationImportJob.java:33)
    at cn.esstx.dzg.runner.tinyrunner.job.OrganizationImportJob.execute_aroundBody1$advice(OrganizationImportJob.java:26)
    at cn.esstx.dzg.runner.tinyrunner.job.OrganizationImportJob.execute(OrganizationImportJob.java:1)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)

 

<无标签>
举报
hengqihengyi
发帖于6个月前 2回/1K+阅

其中,日志打印如下,从获取连接到提交事务报异常中间的时间比较长

dzg-server-runner-prod: 2018-04-13 06:05:41.367 [DefaultQuartzScheduler_Worker-23] INFO  (Organization.java:304) - mysqlName : reportDB
dzg-server-runner-prod: 2018-04-13 06:05:41.368 [DefaultQuartzScheduler_Worker-23] INFO  (Organization.java:310) - 开始获取连接
dzg-server-runner-prod: 2018-04-13 06:36:42.847 [DefaultQuartzScheduler_Worker-23] INFO  (Organization.java:312) - 开始获取PreparedStatement
dzg-server-runner-prod: 2018-04-13 06:36:42.848 [DefaultQuartzScheduler_Worker-23] INFO  (Organization.java:314) - 开始执行
dzg-server-runner-prod: 2018-04-13 06:36:42.857 [DefaultQuartzScheduler_Worker-23] INFO  (Organization.java:316) - 开始关闭
dzg-server-runner-prod: 2018-04-13 06:36:42.857 [DefaultQuartzScheduler_Worker-23] INFO  (Organization.java:318) - 关闭完成
dzg-server-runner-prod: 2018-04-13 06:40:00.001 [DefaultQuartzScheduler_Worker-8] INFO  (UpdateStatusJob.java:19) -
dzg-server-runner-prod: 2018-04-13 06:52:20.033 [DefaultQuartzScheduler_Worker-23] INFO  (OrgDataSyncService.java:50) - 导入机构定时任务:异常:
com.jfinal.plugin.activerecord.ActiveRecordException: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Communications link failure during commit(). Transaction resolution unknown.
at com.jfinal.plugin.activerecord.DbPro.tx(DbPro.java:723)
at com.jfinal.plugin.activerecord.DbPro.tx(DbPro.java:748)
at com.jfinal.plugin.activerecord.Db.tx(Db.java:460)
at cn.esstx.dzg.runner.tinyrunner.model.Organization.saveWithReportDbTx(Organization.java:89)
at cn.esstx.dzg.runner.tinyrunner.service.ds.org.OrganizationService.importData(OrganizationService.java:145)
at cn.esstx.dzg.runner.tinyrunner.service.ds.org.OrgDataSyncService.import2Database(OrgDataSyncService.java:118)
at cn.esstx.dzg.runner.tinyrunner.service.ds.org.OrgDataSyncService.importData(OrgDataSyncService.java:46)
at cn.esstx.dzg.runner.tinyrunner.job.OrganizationImportJob.execute_aroundBody0(OrganizationImportJob.java:33)
at cn.esstx.dzg.runner.tinyrunner.job.OrganizationImportJob.execute_aroundBody1$advice(OrganizationImportJob.java:26)
at cn.esstx.dzg.runner.tinyrunner.job.OrganizationImportJob.execute(OrganizationImportJob.java:1)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Communications link failure during commit(). Transaction resolution unknown.
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:422)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.Util.getInstance(Util.java:387)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:917)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:896)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:885)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
at com.mysql.jdbc.ConnectionImpl.commit(ConnectionImpl.java:1620)
at com.alibaba.druid.pool.DruidPooledConnection.commit(DruidPooledConnection.java:711)
at com.jfinal.plugin.activerecord.DbPro.tx(DbPro.java:713)
... 11 common frames omitted
dzg-server-runner-prod: 2018-04-13 06:52:20.034 [DefaultQuartzScheduler_Worker-23] ERROR (OrganizationImportJob.java:33) - [after throwing]cn.esstx.dzg.runner.tinyrunner.LoggingAspectAround@6965a809:cn.esstx.dzg.runner.tinyrunner.job.OrganizationImportJob.execute
org.quartz.JobExecutionException: java.lang.Exception: 导入机构定时任务
at cn.esstx.dzg.runner.tinyrunner.job.OrganizationImportJob.execute_aroundBody0(OrganizationImportJob.java:35)
at cn.esstx.dzg.runner.tinyrunner.job.OrganizationImportJob.execute_aroundBody1$advice(OrganizationImportJob.java:26)
at cn.esstx.dzg.runner.tinyrunner.job.OrganizationImportJob.execute(OrganizationImportJob.java:1)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
Caused by: java.lang.Exception: 导入机构定时任务
at cn.esstx.dzg.runner.tinyrunner.service.ds.org.OrgDataSyncService.importData(OrgDataSyncService.java:51)
at cn.esstx.dzg.runner.tinyrunner.job.OrganizationImportJob.execute_aroundBody0(OrganizationImportJob.java:33)
... 4 common frames omitted
Caused by: com.jfinal.plugin.activerecord.ActiveRecordException: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Communications link failure during commit(). Transaction resolution unknown.
at com.jfinal.plugin.activerecord.DbPro.tx(DbPro.java:723)
at com.jfinal.plugin.activerecord.DbPro.tx(DbPro.java:748)
at com.jfinal.plugin.activerecord.Db.tx(Db.java:460)
at cn.esstx.dzg.runner.tinyrunner.model.Organization.saveWithReportDbTx(Organization.java:89)
at cn.esstx.dzg.runner.tinyrunner.service.ds.org.OrganizationService.importData(OrganizationService.java:145)
at cn.esstx.dzg.runner.tinyrunner.service.ds.org.OrgDataSyncService.import2Database(OrgDataSyncService.java:118)
at cn.esstx.dzg.runner.tinyrunner.service.ds.org.OrgDataSyncService.importData(OrgDataSyncService.java:46)
... 5 common frames omitted
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Communications link failure during commit(). Transaction resolution unknown.
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:422)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.Util.getInstance(Util.java:387)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:917)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:896)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:885)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
at com.mysql.jdbc.ConnectionImpl.commit(ConnectionImpl.java:1620)
at com.alibaba.druid.pool.DruidPooledConnection.commit(DruidPooledConnection.java:711)
at com.jfinal.plugin.activerecord.DbPro.tx(DbPro.java:713)
... 11 common frames omitted
顶部