`

spring 事务调试

 
阅读更多

                                                       Spring 事务调试 

1, 关于sring怎么去配置事务,网上已有很多了,不在此介绍了,但有个问题,有多少人知道Spring的声明式事务是否已起作用,在此主要介绍怎么调试spring事务 。

 

2, 准备一项目 ,

log4j.xml 中 加入:

 

   <logger name="org.springframework.transaction" additivity="false">
  <level value="debug" />
  把这个改为deubg就可以 
  
  <!-- for development environment -->
  <appender-ref ref="ConsoleAppender" />
 </logger> 
 
 <logger name="org.springframework.transaction.interceptor.TransactionInterceptor" additivity="false">
  <level value="debug" /> 
把这个改为deubg就可以
  
  <!-- for development environment -->
  <appender-ref ref="ConsoleAppender" />
 </logger> 

 

当然,在log4j.properties 中也有相应的,

加入:

log4j.logger.org.springframework=debug  就可以了 

 

3, spring的事务配置方法有N多,

在此列一种:

 

 

<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">    
       <property name="dataSource" ref="dataSource"/>  
   </bean> 

      
    <tx:advice id="txAdvice" transaction-manager="txManager" > 
        <tx:attributes> 
           <tx:method name="save*" propagation="REQUIRED" rollback-for="SqlExecutorException"/> 
           <tx:method name="*" propagation="REQUIRED" read-only="true" no-rollback-for="SqlExecutorException"/>
       </tx:attributes> 
    </tx:advice> 
   <aop:config proxy-target-class="false"> <!-- 是否使用cglib代理 --> 
        <aop:pointcut expression="execution(* org.com.spring.test.ioc01.TestTransManager.*(..))" id="managerTx"/> 
        <aop:advisor advice-ref="txAdvice" pointcut-ref="managerTx"/> 
   </aop:config> 

 

4, 主要程序代码为: 同时 insert 两条,先看看调试时发出的日志,当然也可以是N条

   public void saveTest_1() throws SqlExecutorException
 {
 
      this.sqlExecutor.update("insert into test_1(id,name,code) values(23,'33','454')", null) ;
      this.sqlExecutor.update("insert into test_1(id,name,code) values(5,'33','454')", null) ;
 
 }

:

Creating new transaction with name [org.com.spring.test.ioc01.TestTransManager.saveTest_1]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT,-SqlExecutorException
2012-09-12 05:22:17,655:DEBUG DriverManagerDataSource main - Creating new JDBC DriverManager Connection to [jdbc:oracle:thin:@135.192.71.136:1521:orcl]
2012-09-12 05:22:18,265:DEBUG DataSourceTransactionManager main - Acquired Connection [oracle.jdbc.driver.T4CConnection@dc41c5] for JDBC transaction
2012-09-12 05:22:18,265:DEBUG DataSourceTransactionManager main - Switching JDBC Connection [oracle.jdbc.driver.T4CConnection@dc41c5] to manual commit
2012-09-12 05:22:18,265:DEBUG TransactionSynchronizationManager main - Bound value [org.springframework.jdbc.datasource.ConnectionHolder@5c3987] for key [org.springframework.jdbc.datasource.DriverManagerDataSource@3afb99] to thread [main]
2012-09-12 05:22:18,265:DEBUG TransactionSynchronizationManager main - Initializing transaction synchronization
2012-09-12 05:22:18,265:DEBUG TransactionInterceptor main - Getting transaction for [org.com.spring.test.ioc01.TestTransManager.saveTest_1]
2012-09-12 05:22:18,280:DEBUG JdbcTemplate main - Executing prepared SQL update
2012-09-12 05:22:18,280:DEBUG JdbcTemplate main - Executing prepared SQL statement [insert into test_1(id,name,code) values(23,'33','454')]
2012-09-12 05:22:18,280:DEBUG TransactionSynchronizationManager main - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@5c3987] for key [org.springframework.jdbc.datasource.DriverManagerDataSource@3afb99] bound to thread [main]
2012-09-12 05:22:18,515:DEBUG TransactionSynchronizationManager main - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@5c3987] for key [org.springframework.jdbc.datasource.DriverManagerDataSource@3afb99] bound to thread [main]
2012-09-12 05:22:18,515:DEBUG JdbcTemplate main - SQL update affected 1 rows
2012-09-12 05:22:18,530:DEBUG TransactionSynchronizationManager main - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@5c3987] for key [org.springframework.jdbc.datasource.DriverManagerDataSource@3afb99] bound to thread [main]
2012-09-12 05:22:18,530:DEBUG SpringJdbcSqlExecutor main -  Time-consuming:250ms | insert into test_1(id,name,code) values(23,'33','454')
 SpringJdbcSqlExecutor pringSql console method times is  : 250ms | insert into test_1(id,name,code) values(23,'33','454')
2012-09-12 05:22:18,530:DEBUG JdbcTemplate main - Executing prepared SQL update
2012-09-12 05:22:18,530:DEBUG JdbcTemplate main - Executing prepared SQL statement [insert into test_1(id,name,code) values(24,'33','454')]
2012-09-12 05:22:18,530:DEBUG TransactionSynchronizationManager main - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@5c3987] for key [org.springframework.jdbc.datasource.DriverManagerDataSource@3afb99] bound to thread [main]
2012-09-12 05:22:18,530:DEBUG TransactionSynchronizationManager main - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@5c3987] for key [org.springframework.jdbc.datasource.DriverManagerDataSource@3afb99] bound to thread [main]
2012-09-12 05:22:18,530:DEBUG JdbcTemplate main - SQL update affected 1 rows
2012-09-12 05:22:18,530:DEBUG TransactionSynchronizationManager main - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@5c3987] for key [org.springframework.jdbc.datasource.DriverManagerDataSource@3afb99] bound to thread [main]
2012-09-12 05:22:18,530:DEBUG SpringJdbcSqlExecutor main -  Time-consuming:0ms | insert into test_1(id,name,code) values(24,'33','454')
 SpringJdbcSqlExecutor pringSql console method times is  : 0ms | insert into test_1(id,name,code) values(24,'33','454')
2012-09-12 05:22:18,530:DEBUG TransactionInterceptor main - Completing transaction for [org.com.spring.test.ioc01.TestTransManager.saveTest_1]
2012-09-12 05:22:18,530:DEBUG DataSourceTransactionManager main - Triggering beforeCommit synchronization
2012-09-12 05:22:18,530:DEBUG DataSourceTransactionManager main - Triggering beforeCompletion synchronization
2012-09-12 05:22:18,530:DEBUG DataSourceTransactionManager main - Initiating transaction commit

 

中间SQL打印两次,有一次是本人程序打印出来的,

 

5,再来一个insert 已有的 :

public void saveTest_1() throws SqlExecutorException
 {
 
   this.sqlExecutor.update("insert into test_1(id,name,code) values(25,'33','454')", null) ;
25是新加的
   this.sqlExecutor.update("insert into test_1(id,name,code) values(24,'33','454')", null) ;
24是已有的
 
 }  看看 25会不会加进去

Creating new transaction with name [org.com.spring.test.ioc01.TestTransManager.saveTest_1]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT,-SqlExecutorException
2012-09-12 05:25:12,468:DEBUG DriverManagerDataSource main - Creating new JDBC DriverManager Connection to [jdbc:oracle:thin:@135.192.71.136:1521:orcl]
2012-09-12 05:25:13,077:DEBUG DataSourceTransactionManager main - Acquired Connection [oracle.jdbc.driver.T4CConnection@14e113b] for JDBC transaction
2012-09-12 05:25:13,077:DEBUG DataSourceTransactionManager main - Switching JDBC Connection [oracle.jdbc.driver.T4CConnection@14e113b] to manual commit
2012-09-12 05:25:13,077:DEBUG TransactionSynchronizationManager main - Bound value [org.springframework.jdbc.datasource.ConnectionHolder@147917a] for key [org.springframework.jdbc.datasource.DriverManagerDataSource@c4d04d] to thread [main]
2012-09-12 05:25:13,077:DEBUG TransactionSynchronizationManager main - Initializing transaction synchronization
2012-09-12 05:25:13,077:DEBUG TransactionInterceptor main - Getting transaction for [org.com.spring.test.ioc01.TestTransManager.saveTest_1]
2012-09-12 05:25:13,093:DEBUG JdbcTemplate main - Executing prepared SQL update
2012-09-12 05:25:13,093:DEBUG JdbcTemplate main - Executing prepared SQL statement [insert into test_1(id,name,code) values(25,'33','454')]
2012-09-12 05:25:13,093:DEBUG TransactionSynchronizationManager main - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@147917a] for key [org.springframework.jdbc.datasource.DriverManagerDataSource@c4d04d] bound to thread [main]
2012-09-12 05:25:13,327:DEBUG TransactionSynchronizationManager main - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@147917a] for key [org.springframework.jdbc.datasource.DriverManagerDataSource@c4d04d] bound to thread [main]
2012-09-12 05:25:13,358:DEBUG JdbcTemplate main - SQL update affected 1 rows
2012-09-12 05:25:13,374:DEBUG TransactionSynchronizationManager main - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@147917a] for key [org.springframework.jdbc.datasource.DriverManagerDataSource@c4d04d] bound to thread [main]
2012-09-12 05:25:13,374:DEBUG SpringJdbcSqlExecutor main -  Time-consuming:297ms | insert into test_1(id,name,code) values(25,'33','454')
 SpringJdbcSqlExecutor pringSql console method times is  : 297ms | insert into test_1(id,name,code) values(25,'33','454')
2012-09-12 05:25:13,499:DEBUG JdbcTemplate main - Executing prepared SQL update
2012-09-12 05:25:13,499:DEBUG JdbcTemplate main - Executing prepared SQL statement [insert into test_1(id,name,code) values(24,'33','454')]
2012-09-12 05:25:13,499:DEBUG TransactionSynchronizationManager main - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@147917a] for key [org.springframework.jdbc.datasource.DriverManagerDataSource@c4d04d] bound to thread [main]
2012-09-12 05:25:13,499:DEBUG TransactionSynchronizationManager main - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@147917a] for key [org.springframework.jdbc.datasource.DriverManagerDataSource@c4d04d] bound to thread [main]
2012-09-12 05:25:13,530:DEBUG TransactionSynchronizationManager main - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@147917a] for key [org.springframework.jdbc.datasource.DriverManagerDataSource@c4d04d] bound to thread [main]
2012-09-12 05:25:13,530:INFO XmlBeanDefinitionReader main - Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml]
2012-09-12 05:25:13,530:DEBUG DefaultDocumentLoader main - Using JAXP provider [com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl]
2012-09-12 05:25:13,530:DEBUG BeansDtdResolver main - Trying to resolve XML entity with public ID [-//SPRING//DTD BEAN 2.0//EN] and system ID [http://www.springframework.org/dtd/spring-beans-2.0.dtd]
2012-09-12 05:25:13,530:DEBUG BeansDtdResolver main - Trying to locate [spring-beans-2.0.dtd] in Spring jar
2012-09-12 05:25:13,546:DEBUG BeansDtdResolver main - Found beans DTD [http://www.springframework.org/dtd/spring-beans-2.0.dtd] in classpath: spring-beans-2.0.dtd
2012-09-12 05:25:13,546:DEBUG DefaultBeanDefinitionDocumentReader main - Loading bean definitions
2012-09-12 05:25:13,562:DEBUG DefaultListableBeanFactory main - Creating shared instance of singleton bean 'DB2'
2012-09-12 05:25:13,562:DEBUG DefaultListableBeanFactory main - Creating instance of bean 'DB2'
2012-09-12 05:25:13,562:DEBUG DefaultListableBeanFactory main - Eagerly caching bean 'DB2' to allow for resolving potential circular references
2012-09-12 05:25:13,562:DEBUG CachedIntrospectionResults main - Getting BeanInfo for class [org.springframework.jdbc.support.SQLErrorCodes]
[org.springframework.beans.propertyeditors.StringArrayPropertyEditor@14e45b3]
2012-09-12 05:25:13,577:DEBUG DefaultListableBeanFactory main - Finished creating instance of bean 'DB2'

..................................

2012-09-12 05:25:14,140:INFO SQLErrorCodesFactory main - SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase]
2012-09-12 05:25:14,140:DEBUG SQLErrorCodesFactory main - Looking up default SQLErrorCodes for DataSource [org.springframework.jdbc.datasource.DriverManagerDataSource@c4d04d]
2012-09-12 05:25:14,140:DEBUG TransactionSynchronizationManager main - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@147917a] for key [org.springframework.jdbc.datasource.DriverManagerDataSource@c4d04d] bound to thread [main]
2012-09-12 05:25:14,140:DEBUG TransactionSynchronizationManager main - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@147917a] for key [org.springframework.jdbc.datasource.DriverManagerDataSource@c4d04d] bound to thread [main]
2012-09-12 05:25:14,140:DEBUG SQLErrorCodesFactory main - Database product name cached for DataSource [org.springframework.jdbc.datasource.DriverManagerDataSource@c4d04d]: name is 'Oracle'
2012-09-12 05:25:14,140:DEBUG SQLErrorCodesFactory main - SQL error codes for 'Oracle' found
2012-09-12 05:25:14,140:DEBUG SQLErrorCodeSQLExceptionTranslator main - Translating SQLException with SQL state '23000', error code '1', message [ORA-00001: 违反唯一约束条件 (TELANT0815.SYS_C0056420)
]; SQL was [insert into test_1(id,name,code) values(24,'33','454')] for task [PreparedStatementCallback]
2012-09-12 05:25:14,140:DEBUG SpringJdbcSqlExecutor main -  Time-consuming:0ms | insert into test_1(id,name,code) values(24,'33','454')
 SpringJdbcSqlExecutor pringSql console method times is  : 0ms | insert into test_1(id,name,code) values(24,'33','454')
2012-09-12 05:25:14,140:DEBUG TransactionInterceptor main - Completing transaction for [org.com.spring.test.ioc01.TestTransManager.saveTest_1] after exception: oss.com.spring.engin.jdbc.SqlExecutorException:
2012-09-12 05:25:14,140:DEBUG RuleBasedTransactionAttribute main - Applying rules to determine whether transaction should rollback on oss.com.spring.engin.jdbc.SqlExecutorException:
2012-09-12 05:25:14,140:DEBUG RuleBasedTransactionAttribute main - Winning rollback rule is: RollbackRuleAttribute with pattern [SqlExecutorException]
2012-09-12 05:25:14,140:DEBUG DataSourceTransactionManager main - Triggering beforeCompletion
synchronization
2012-09-12 05:25:14,140:DEBUG DataSourceTransactionManager main - Initiating transaction rollback
2012-09-12 05:25:14,140:DEBUG DataSourceTransactionManager main - Rolling back JDBC transaction on Connection [oracle.jdbc.driver.T4CConnection@14e113b]
2012-09-12 05:25:14,140:DEBUG DataSourceTransactionManager main - Triggering afterCompletion synchronization
2012-09-12 05:25:14,140:DEBUG TransactionSynchronizationManager main - Clearing transaction synchronization
2012-09-12 05:25:14,140:DEBUG TransactionSynchronizationManager main - Removed value [org.springframework.jdbc.datasource.ConnectionHolder@147917a] for key [org.springframework.jdbc.datasource.DriverManagerDataSource@c4d04d] from thread [main]
2012-09-12 05:25:14,140:DEBUG DataSourceTransactionManager main - Releasing JDBC Connection [oracle.jdbc.driver.T4CConnection@14e113b] after transaction
2012-09-12 05:25:14,140:DEBUG DataSourceUtils main - Returning JDBC Connection to DataSource

2012-09-12 05:25:14,140:DEBUG DataSourceTransactionManager main - Releasing JDBC Connection [oracle.jdbc.driver.T4CConnection@14e113b] after transaction

 

可以看到 已经 rollback 了,查看库中也没  insert 进去

 

6,说明:

    本人使用的是Oracle ,如果你使用的数据库本身不支持事务的话,光依靠 Spring的事务也没有用,Spring也是使用底层数据库的事务机制。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

    Spring事务管理开发必备jar包

    本资源包提供了进行Spring事务管理开发所需的所有关键库,包括框架基础、核心组件、AOP(面向切面编程)支持、日志处理、编译工具以及与数据库交互的相关jar包。下面将对这些知识点进行详细解释: 1. **Spring框架*...

    Spring事务小demo

    6. **测试与调试**:在"Spring事务小demo"项目中,可以编写JUnit测试类来验证事务的正确性。测试类中,使用@Test注解并配置@Test rollback属性,可以确保每次测试结束后,事务都会被回滚,保持数据库的原始状态。 ...

    Spring事务管理A方法内部调用B方法的回滚问题测试代码

    在Spring框架中,事务管理是核心特性之一,用于确保数据操作的一致性和完整性。当一个方法(A方法)内部调用另一个方法(B方法)时,可能会遇到事务控制...这个示例代码对于理解和调试Spring事务管理的问题非常有帮助。

    spring hibernate 事务管理学习笔记(一)

    在实际开发中,理解这部分源码有助于我们更深入地掌握Spring事务管理的工作原理。 至于工具,开发者可以使用诸如IntelliJ IDEA这样的IDE,其中集成的调试工具可以帮助我们跟踪代码执行流程,查看事务状态的变化,...

    spring声明式事务处理demo

    首先,我们要理解Spring事务管理的两种主要方式:编程式事务管理和声明式事务管理。编程式事务管理通常通过AOP(面向切面编程)的TransactionTemplate或PlatformTransactionManager接口直接在代码中控制事务,而声明...

    Spring事务相关类Uml图

    理解Spring事务管理的相关类和它们之间的关系对于优化和调试事务处理代码至关重要。通过深入学习这些类的职责和交互方式,开发者可以更好地利用Spring的事务管理功能,确保应用程序的数据一致性。

    解决osgi spring 事务配置问题

    Spring Dynamic Modules(Spring-DM,现在已经被Spring OSGi取代)提供了一种方式来处理这种情况,它可以帮助我们在OSGi环境中创建透明的Spring事务代理。 此外,我们需要确保所有涉及事务的服务都在同一个...

    spring事物传播测试表

    事务传播行为(Transaction Propagation)是Spring事务管理中一个关键的概念,用于定义在一个事务方法被调用时,如何与当前运行的事务进行交互。本文将深入探讨“Spring事物传播测试表”所涉及的知识点。 首先,...

    spring事务精髓

    在Spring框架中,事务管理是核心特性之一,它允许开发者以声明式或编程式的方式处理事务。Spring通过AOP(面向切面编程)实现...理解Spring事务管理的工作原理,对于优化和调试分布式系统中的事务问题具有重要意义。

    Spring源码工程,可以断点调试,外加一个AOP的小例子(不带JAR包)

    本资源提供了一个Spring的源码工程,允许开发者进行断点调试,这对于理解Spring的工作原理和深入学习非常有帮助。同时,附带了一个AOP的小例子,便于实践和学习。 首先,让我们详细讨论Spring的核心概念——依赖...

    Spring整合JMS(四)——事务管理

    为了调试和监控事务,Spring还提供了`PlatformTransactionManager`接口,我们可以实现这个接口并结合日志记录,以便跟踪事务的生命周期。 总的来说,Spring整合JMS的事务管理是一个复杂但关键的过程,它涉及到消息...

    Spring_任务调试源码

    在SSH集成中,Spring可以帮助管理Hibernate的SessionFactory,使得在多个地方共享Session变得简单,同时Spring的事务管理可以与Hibernate的事务处理相结合,提供更高级别的事务控制。 在压缩包中的“spring_ch07...

    Spring/SpringMVC/MyBatis整合+事务回滚

    8. **测试与调试**:完成整合后,通过编写单元测试和集成测试来验证配置是否正确,确保事务管理和回滚功能正常工作。 以上就是Spring、SpringMVC和MyBatis整合及事务回滚的基础知识。这个主题涉及到的技能涵盖了...

    spring,hibernate整合实现事务管理(MethodInterceptor)

    - Spring事务管理的核心接口包括`PlatformTransactionManager`和`TransactionDefinition`。前者负责事务的创建和管理,后者用于定义事务的属性,如隔离级别、超时时间等。 2. **Hibernate事务管理**: - ...

    spring+druid+AtomikosDataSource实现多数据源切换及分布式事务控制

    Spring的`@Transactional`注解可以用来开启和管理这些分布式事务,确保事务的ACID特性。 7. **代码示例** 创建多数据源的配置文件,包括AtomikosDataSourceBean的定义、TransactionManager的配置以及...

    无处不在的Spring AOP事务及踩过的坑

    Spring AOP事务管理是Java开发中的重要组成部分,它极大地简化了在分布式系统中对事务的处理。...在遇到问题时,及时查阅文档,分析调用链路,调试事务行为,通常都能找到问题的根源并找到解决之道。

    spring+jotm+ibatis+mysql实现JTA分布式事务

    项目文件“AjtaAbatisTest”可能是测试类或者配置文件,用于验证和调试整个分布式事务的实现。通常,这样的测试会模拟不同的事务场景,例如并发更新、异常处理等,以确保在各种情况下事务都能正确地进行提交或回滚。...

Global site tag (gtag.js) - Google Analytics