這篇文章主要介紹“Spring Transactional事務(wù)和日志的用法”,在日常操作中,相信很多人在Spring Transactional事務(wù)和日志的用法問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Spring Transactional事務(wù)和日志的用法”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、成都外貿(mào)網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的郟縣網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
1、主要代碼
// 切換數(shù)據(jù)源 @DataSourceRouting // 開(kāi)始事務(wù) @Transactional(rollbackFor = Exception.class) public boolean test1(Long userId, ListlabelList) { biz1(userId, labelList); biz2(userId, labelList); return true; } public boolean biz1(Long userId, List labelList) { log.info("》》》》》》》》插入 FamilyPlanLabelDetail 》》》 開(kāi)始"); FamilyPlanLabelDetail familyPlanLabelDetail = new FamilyPlanLabelDetail(); familyPlanLabelDetail.setUserId(userId); familyPlanLabelDetail.setCreateTime(new Date()); int numLabelDetail = familyPlanDao.saveFamilyPlanLabelDetail(familyPlanLabelDetail); log.info("》》》》》》》》插入 FamilyPlanLabelDetail 》》》 結(jié)束"); return true; } public boolean biz2(Long userId, List labelList) throws Exception { log.info("》》》》》》》》插入 FamilyPlanLabel 》》》 開(kāi)始"); List list = new ArrayList(); for (String labelName : labelList) { FamilyPlanLabel familyPlanLabel = new FamilyPlanLabel(); familyPlanLabel.setUserId(userId); familyPlanLabel.setLabelName(labelName); familyPlanLabel.setType(FamilyPlanLabelTypeEnum.SYS_ADD.getCode()); familyPlanLabel.setStatus(FamilyPlanLabelStatusEnum.USE.getCode()); familyPlanLabel.setCreateTime(new Date()); familyPlanLabel.setUpdateTime(new Date()); list.add(familyPlanLabel); } int labelNum = familyPlanDao.saveFamilyPlanLabelByBatch(list); if (true) { // 模擬出異常 throw new Exception("手動(dòng)拋異常"); } log.info("》》》》》》》》插入 FamilyPlanLabel 》》》 結(jié)束"); return true; }
2 開(kāi)事務(wù)
2.1 開(kāi)事務(wù),正常的日志
// 方法上有注解 @DataSourceRouting @Transactional com.moon.core.dbUtils.DataSourceAspect 【數(shù)據(jù)源處理】com.moon.client.transaction.manager.FamilyPlanTransactionManager.test1(...)方法執(zhí)行開(kāi)始執(zhí)行,數(shù)據(jù)源切換到:master com.moon.client.transaction.manager.FamilyPlanTransactionManager 》》》》》》》》查詢 FamilyPlanLabelDetail 》》》 開(kāi)始 // 因?yàn)樵谑聞?wù)中,切換數(shù)據(jù)源并沒(méi)有用 com.moon.core.dbUtils.DataSourceAspect 【數(shù)據(jù)源處理】com.moon.core.dao.FamilyPlanDao.queryFamilyPlanLabelDetailByUserId(...)方法執(zhí)行開(kāi)始執(zhí)行,沒(méi)有配置數(shù)據(jù)源,切換到默認(rèn):master // 開(kāi)啟事務(wù) org.mybatis.spring.SqlSessionUtils Creating a new SqlSession org.mybatis.spring.SqlSessionUtils Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@cdf939b] org.mybatis.spring.transaction.SpringManagedTransaction JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@eeede52] will be managed by Spring com.moon.core.dao.FamilyPlanDao.queryFamilyPlanLabelDetailByUserId ooo Using Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@eeede52] com.moon.core.dao.FamilyPlanDao.queryFamilyPlanLabelDetailByUserId ==> Preparing: SELECT id, user_id AS userId, create_time AS createTime FROM t_detail WHERE user_id = ? com.moon.core.dao.FamilyPlanDao.queryFamilyPlanLabelDetailByUserId ==> Parameters: 1(Long) org.mybatis.spring.SqlSessionUtils Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@cdf939b] com.moon.core.dbUtils.DataSourceAspect 【數(shù)據(jù)源處理】com.moon.core.dao.FamilyPlanDao.queryFamilyPlanLabelDetailByUserId(...)方法執(zhí)行完成,清理數(shù)據(jù)源 com.moon.client.transaction.manager.FamilyPlanTransactionManager 》》》》》》》》查詢 FamilyPlanLabelDetail 》》》 結(jié)束 com.moon.client.transaction.manager.FamilyPlanTransactionManager 》》》》》》》》插入 FamilyPlanLabelDetail 》》》 開(kāi)始 // 因?yàn)樵谑聞?wù)中,切換數(shù)據(jù)源并沒(méi)有用 com.moon.core.dbUtils.DataSourceAspect 【數(shù)據(jù)源處理】com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelDetail(...)方法執(zhí)行開(kāi)始執(zhí)行,沒(méi)有配置數(shù)據(jù)源,切換到默認(rèn):master org.mybatis.spring.SqlSessionUtils Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@cdf939b] from current transaction com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelDetail ooo Using Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@eeede52] com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelDetail ==> Preparing: INSERT INTO t_detail ( user_id, create_time ) VALUES ( ?, ? ) com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelDetail ==> Parameters: 1(Long), 2019-09-16 12:02:00.075(Timestamp) org.mybatis.spring.SqlSessionUtils Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@cdf939b] com.moon.core.dbUtils.DataSourceAspect 【數(shù)據(jù)源處理】com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelDetail(...)方法執(zhí)行完成,清理數(shù)據(jù)源 com.moon.client.transaction.manager.FamilyPlanTransactionManager 》》》》》》》》插入 FamilyPlanLabelDetail 》》》 結(jié)束 com.moon.client.transaction.manager.FamilyPlanTransactionManager 》》》》》》》》插入 FamilyPlanLabel 》》》 開(kāi)始 // 因?yàn)樵谑聞?wù)中,切換數(shù)據(jù)源并沒(méi)有用 com.moon.core.dbUtils.DataSourceAspect 【數(shù)據(jù)源處理】com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelByBatch(...)方法執(zhí)行開(kāi)始執(zhí)行,沒(méi)有配置數(shù)據(jù)源,切換到默認(rèn):master org.mybatis.spring.SqlSessionUtils Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@cdf939b] from current transaction com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelByBatch ooo Using Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@eeede52] com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelByBatch ==> Preparing: INSERT INTO t_label ( user_id, label_name, type, status, create_time, update_time ) VALUES ( ?, ?, ?, ?, ?, ? ) com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelByBatch ==> Parameters: 1(Long), test1342966(String), 1(Integer), 1(Integer), 2019-09-16 12:02:00.917(Timestamp), 2019-09-16 12:02:00.917(Timestamp) org.mybatis.spring.SqlSessionUtils Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@cdf939b] com.moon.core.dbUtils.DataSourceAspect 【數(shù)據(jù)源處理】com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelByBatch(...)方法執(zhí)行完成,清理數(shù)據(jù)源 com.moon.client.transaction.manager.FamilyPlanTransactionManager 》》》》》》》》插入 FamilyPlanLabel 》》》 結(jié)束 // 提交事務(wù) org.mybatis.spring.SqlSessionUtils Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@cdf939b] org.mybatis.spring.SqlSessionUtils Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@cdf939b] com.moon.core.dbUtils.DataSourceAspect 【數(shù)據(jù)源處理】com.moon.client.transaction.manager.FamilyPlanTransactionManager.test1(...)方法執(zhí)行完成,清理數(shù)據(jù)源
注意:
(1)org.mybatis.spring.transaction.SpringManagedTransaction JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@3d2fb2bf] will be managed by Spring
(2)org.mybatis.spring.SqlSessionUtils Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@251c52c5]
2.2 開(kāi)事務(wù),出現(xiàn)異?;貪L的日志
com.moon.core.dbUtils.DataSourceAspect 【數(shù)據(jù)源處理】com.moon.client.transaction.manager.FamilyPlanTransactionManager.test1(...)方法執(zhí)行開(kāi)始執(zhí)行,數(shù)據(jù)源切換到:master com.moon.client.transaction.manager.FamilyPlanTransactionManager 》》》》》》》》插入 FamilyPlanLabelDetail 》》》 開(kāi)始 com.moon.core.dbUtils.DataSourceAspect 【數(shù)據(jù)源處理】com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelDetail(...)方法執(zhí)行開(kāi)始執(zhí)行,沒(méi)有配置數(shù)據(jù)源,切換到默認(rèn):master org.mybatis.spring.SqlSessionUtils Creating a new SqlSession org.mybatis.spring.SqlSessionUtils Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@67334835] org.mybatis.spring.transaction.SpringManagedTransaction JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@3d2fb2bf] will be managed by Spring com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelDetail ooo Using Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@3d2fb2bf] com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelDetail ==> Preparing: INSERT INTO t_detail ( user_id, create_time ) VALUES ( ?, ? ) com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelDetail ==> Parameters: 1(Long), 2019-09-16 10:26:50.722(Timestamp) org.mybatis.spring.SqlSessionUtils Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@67334835] com.moon.core.dbUtils.DataSourceAspect 【數(shù)據(jù)源處理】com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelDetail(...)方法執(zhí)行完成,清理數(shù)據(jù)源 com.moon.client.transaction.manager.FamilyPlanTransactionManager 》》》》》》》》插入 FamilyPlanLabelDetail 》》》 結(jié)束 com.moon.client.transaction.manager.FamilyPlanTransactionManager 》》》》》》》》插入 FamilyPlanLabel 》》》 開(kāi)始 com.moon.core.dbUtils.DataSourceAspect 【數(shù)據(jù)源處理】com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelByBatch(...)方法執(zhí)行開(kāi)始執(zhí)行,沒(méi)有配置數(shù)據(jù)源,切換到默認(rèn):master org.mybatis.spring.SqlSessionUtils Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@67334835] from current transaction com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelByBatch ooo Using Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@3d2fb2bf] com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelByBatch ==> Preparing: INSERT INTO t_label ( user_id, label_name, type, status, create_time, update_time ) VALUES ( ?, ?, ?, ?, ?, ? ) com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelByBatch ==> Parameters: 1(Long), test1591984(String), 1(Integer), 1(Integer), 2019-09-16 10:27:12.847(Timestamp), 2019-09-16 10:27:12.847(Timestamp) org.mybatis.spring.SqlSessionUtils Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@67334835] com.moon.core.dbUtils.DataSourceAspect 【數(shù)據(jù)源處理】com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelByBatch(...)方法執(zhí)行完成,清理數(shù)據(jù)源 org.mybatis.spring.SqlSessionUtils Transaction synchronization rolling back SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@67334835] org.mybatis.spring.SqlSessionUtils Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@67334835] com.moon.core.dbUtils.DataSourceAspect 【數(shù)據(jù)源處理】com.moon.client.transaction.manager.FamilyPlanTransactionManager.test1(...)方法執(zhí)行完成,清理數(shù)據(jù)源 com.moon.home.controller.AtestController 出現(xiàn)了異常 java.lang.RuntimeException: 家庭計(jì)劃,初始化賬戶系統(tǒng)默認(rèn)賬戶類型.出現(xiàn)異常 at com.moon.client.transaction.manager.FamilyPlanTransactionManager.biz2(FamilyPlanTransactionManager.java:367) ~[CKclient-1.0.0-SNAPSHOT.jar:na] at com.moon.client.transaction.manager.FamilyPlanTransactionManager.test1(FamilyPlanTransactionManager.java:382) ~[CKclient-1.0.0-SNAPSHOT.jar:na] at com.moon.client.transaction.manager.FamilyPlanTransactionManager$$FastClassBySpringCGLIB$$728e76cf.invoke() ~[CKclient-1.0.0-SNAPSHOT.jar:na] at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.3.11.RELEASE.jar:4.3.11.RELEASE] at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738) ~[spring-aop-4.3.11.RELEASE.jar:4.3.11.RELEASE]
注意:
(1)org.mybatis.spring.transaction.SpringManagedTransaction JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@3d2fb2bf] will be managed by Spring
(2)org.mybatis.spring.SqlSessionUtils Transaction synchronization rolling back SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@67334835]
2.3 開(kāi)啟事務(wù)總結(jié)
(1)方法上加了@Transactional ,在進(jìn)入方法的業(yè)務(wù)代碼前(本例就是進(jìn)入 test1() 的業(yè)務(wù)代碼前)已經(jīng)決定了使用哪個(gè)數(shù)據(jù)源,在業(yè)務(wù)方法 biz1()或biz2()內(nèi)切換數(shù)據(jù)源是無(wú)效的。
(2) 加了@Transactional注解,如果在進(jìn)入test1()方法前指定了數(shù)據(jù)源,test1() 沒(méi)有在切面中切換數(shù)據(jù)源,則以進(jìn)入test1()方法前用的數(shù)據(jù)源為準(zhǔn);如果沒(méi)有指定數(shù)據(jù)源,以系統(tǒng)配置的默認(rèn)數(shù)據(jù)源。
(3)并不是加了@Transactional 注解,在進(jìn)入test1() 方法后就開(kāi)啟事務(wù),是有SQL執(zhí)行時(shí)才會(huì)開(kāi)啟事務(wù)。
3 不開(kāi)事務(wù)
3.1 不開(kāi)事務(wù),執(zhí)行成功日志
// 進(jìn)入 biz1(...)方法 com.moon.client.transaction.manager.FamilyPlanTransactionManager 》》》》》》》》插入 FamilyPlanLabelDetail 》》》 開(kāi)始 com.moon.core.dbUtils.DataSourceAspect 【數(shù)據(jù)源處理】com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelDetail(...)方法執(zhí)行開(kāi)始執(zhí)行,沒(méi)有配置數(shù)據(jù)源,切換到默認(rèn):master org.mybatis.spring.SqlSessionUtils Creating a new SqlSession org.mybatis.spring.SqlSessionUtils SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@57494412] was not registered for synchronization because synchronization is not active org.mybatis.spring.transaction.SpringManagedTransaction JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@320e24b0] will not be managed by Spring com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelDetail ooo Using Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@320e24b0] com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelDetail ==> Preparing: INSERT INTO t_detail ( user_id, create_time ) VALUES ( ?, ? ) com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelDetail ==> Parameters: 1(Long), 2019-09-17 11:06:00.781(Timestamp) org.mybatis.spring.SqlSessionUtils Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@57494412] com.moon.core.dbUtils.DataSourceAspect 【數(shù)據(jù)源處理】com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelDetail(...)方法執(zhí)行完成,清理數(shù)據(jù)源 com.moon.client.transaction.manager.FamilyPlanTransactionManager 》》》》》》》》插入 FamilyPlanLabelDetail 》》》 結(jié)束 // 進(jìn)入 biz2(...)方法 com.moon.client.transaction.manager.FamilyPlanTransactionManager 》》》》》》》》插入 FamilyPlanLabel 》》》 開(kāi)始 com.moon.core.dbUtils.DataSourceAspect 【數(shù)據(jù)源處理】com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelByBatch(...)方法執(zhí)行開(kāi)始執(zhí)行,沒(méi)有配置數(shù)據(jù)源,切換到默認(rèn):master org.mybatis.spring.SqlSessionUtils Creating a new SqlSession org.mybatis.spring.SqlSessionUtils SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5471460] was not registered for synchronization because synchronization is not active org.mybatis.spring.transaction.SpringManagedTransaction JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@320e24b0] will not be managed by Spring com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelByBatch ooo Using Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@320e24b0] com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelByBatch ==> Preparing: INSERT INTO t_label ( user_id, label_name, type, status, create_time, update_time ) VALUES ( ?, ?, ?, ?, ?, ? ) com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelByBatch ==> Parameters: 1(Long), test1(String), 1(Integer), 1(Integer), 2019-09-17 11:06:01.056(Timestamp), 2019-09-17 11:06:01.056(Timestamp) org.mybatis.spring.SqlSessionUtils Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5471460] com.moon.core.dbUtils.DataSourceAspect 【數(shù)據(jù)源處理】com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelByBatch(...)方法執(zhí)行完成,清理數(shù)據(jù)源 com.moon.client.transaction.manager.FamilyPlanTransactionManager 》》》》》》》》插入 FamilyPlanLabel 》》》 結(jié)束
3.2 不開(kāi)事務(wù),拋出異常日志
// 進(jìn)入 biz1(...)方法 com.moon.client.transaction.manager.FamilyPlanTransactionManager 》》》》》》》》插入 FamilyPlanLabelDetail 》》》 開(kāi)始 com.moon.core.dbUtils.DataSourceAspect 【數(shù)據(jù)源處理】com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelDetail(...)方法執(zhí)行開(kāi)始執(zhí)行,沒(méi)有配置數(shù)據(jù)源,切換到默認(rèn):master org.mybatis.spring.SqlSessionUtils Creating a new SqlSession org.mybatis.spring.SqlSessionUtils SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@65ad141b] was not registered for synchronization because synchronization is not active org.mybatis.spring.transaction.SpringManagedTransaction JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@320e24b0] will not be managed by Spring com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelDetail ooo Using Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@320e24b0] com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelDetail ==> Preparing: INSERT INTO t_detail ( user_id, create_time ) VALUES ( ?, ? ) com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelDetail ==> Parameters: 1(Long), 2019-09-17 10:55:05.748(Timestamp) org.mybatis.spring.SqlSessionUtils Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@65ad141b] com.moon.core.dbUtils.DataSourceAspect 【數(shù)據(jù)源處理】com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelDetail(...)方法執(zhí)行完成,清理數(shù)據(jù)源 com.moon.client.transaction.manager.FamilyPlanTransactionManager 》》》》》》》》插入 FamilyPlanLabelDetail 》》》 結(jié)束 // 進(jìn)入 biz2(...)方法 com.moon.client.transaction.manager.FamilyPlanTransactionManager 》》》》》》》》插入 FamilyPlanLabel 》》》 開(kāi)始 com.moon.core.dbUtils.DataSourceAspect 【數(shù)據(jù)源處理】com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelByBatch(...)方法執(zhí)行開(kāi)始執(zhí)行,沒(méi)有配置數(shù)據(jù)源,切換到默認(rèn):master org.mybatis.spring.SqlSessionUtils Creating a new SqlSession org.mybatis.spring.SqlSessionUtils SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4d1121df] was not registered for synchronization because synchronization is not active org.mybatis.spring.transaction.SpringManagedTransaction JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@320e24b0] will not be managed by Spring com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelByBatch ooo Using Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@320e24b0] com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelByBatch ==> Preparing: INSERT INTO t_label ( user_id, label_name, type, status, create_time, update_time ) VALUES ( ?, ?, ?, ?, ?, ? ) com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelByBatch ==> Parameters: 1(Long), test1(String), 1(Integer), 1(Integer), 2019-09-17 10:55:06.6(Timestamp), 2019-09-17 10:55:06.6(Timestamp) org.mybatis.spring.SqlSessionUtils Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4d1121df] com.moon.core.dbUtils.DataSourceAspect 【數(shù)據(jù)源處理】com.moon.core.dao.FamilyPlanDao.saveFamilyPlanLabelByBatch(...)方法執(zhí)行完成,清理數(shù)據(jù)源 // biz2里的saveFamilyPlanLabelByBatch()方法拋出異常 com.moon.appserver.controller.privates.TestTranController java.lang.Exception: 手動(dòng)拋異常 at com.moon.client.transaction.manager.FamilyPlanTransactionManager.biz2(FamilyPlanTransactionManager.java:366) ~[CKclient-1.0.0-SNAPSHOT.jar:na] at com.moon.client.transaction.manager.FamilyPlanTransactionManager.test1(FamilyPlanTransactionManager.java:380) ~[CKclient-1.0.0-SNAPSHOT.jar:na] at com.moon.client.transaction.manager.FamilyPlanTransactionManager.test2(FamilyPlanTransactionManager.java:385) ~[CKclient-1.0.0-SNAPSHOT.jar:na] at com.moon.client.transaction.manager.FamilyPlanTransactionManager$$FastClassBySpringCGLIB$$728e76cf.invoke() ~[CKclient-1.0.0-SNAPSHOT.jar:na] at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.3.11.RELEASE.jar:4.3.11.RELEASE] at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738) ~[spring-aop-4.3.11.RELEASE.jar:4.3.11.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.3.11.RELEASE.jar:4.3.11.RELEASE] at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-4.3.11.RELEASE.jar:4.3.11.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.11.RELEASE.jar:4.3.11.RELEASE] at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673) ~[spring-aop-4.3.11.RELEASE.jar:4.3.11.RELEASE] at com.moon.client.transaction.manager.FamilyPlanTransactionManager$$EnhancerBySpringCGLIB$$d931e707.test2( ) ~[CKclient-1.0.0-SNAPSHOT.jar:na] at com.moon.appserver.controller.privates.TestTranController.test(TestTranController.java:29) ~[classes/:na]
到此,關(guān)于“Spring Transactional事務(wù)和日志的用法”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!