要管理事务的service方法注入DataSourceTransactionManager
方法中:
能回滚,结果如下:
[quote]
2011-10-12 17:28:35,286 [org.springframework.jdbc.datasource.DataSourceUtils]-[DEBUG] Fetching JDBC Connection from DataSource
2011-10-12 17:28:35,286 [org.springframework.jdbc.datasource.DriverManagerDataSource]-[DEBUG] Creating new JDBC DriverManager Connection to [jdbc:mysql://localhost:3306/test]
2011-10-12 17:28:35,700 [org.mybatis.spring.SqlSessionUtils]-[DEBUG] Creating SqlSession with JDBC Connection [com.mysql.jdbc.JDBC4Connection@dd5f6d]
2011-10-12 17:28:35,706 [java.sql.Connection]-[DEBUG] ooo Connection Opened
2011-10-12 17:28:35,713 [org.mybatis.spring.transaction.SpringManagedTransaction]-[DEBUG] JDBC Connection [com.mysql.jdbc.JDBC4Connection@dd5f6d] will not be managed by Spring
2011-10-12 17:28:35,731 [org.mybatis.spring.SqlSessionUtils]-[DEBUG] SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@bfd849] was not registered for synchronization because synchronization is not active
2011-10-12 17:28:35,922 [java.sql.PreparedStatement]-[DEBUG] ==> Executing: select id, nick, psw, is_sync as isSync, is_validate_psw as isValidatePsw, init_date as initDate, version_id as versionId from users where nick = ?
2011-10-12 17:28:35,922 [java.sql.PreparedStatement]-[DEBUG] ==> Parameters: test(String)
2011-10-12 17:28:35,946 [org.mybatis.spring.SqlSessionUtils]-[DEBUG] Closing no transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@bfd849]
2011-10-12 17:28:35,948 [org.springframework.jdbc.datasource.DataSourceUtils]-[DEBUG] Returning JDBC Connection to DataSource
2011-10-12 17:28:35,953 [org.springframework.web.servlet.DispatcherServlet]-[DEBUG] Rendering view [org.springframework.web.servlet.view.InternalResourceView: unnamed; URL [init]] in DispatcherServlet with name 'dispatcher'
2011-10-12 17:28:35,953 [org.springframework.web.servlet.view.InternalResourceView]-[DEBUG] Forwarding to resource [init] in InternalResourceView 'null'
2011-10-12 17:28:35,954 [org.springframework.web.servlet.DispatcherServlet]-[DEBUG] DispatcherServlet with name 'dispatcher' processing GET request for [/test/sync/init]
2011-10-12 17:28:35,954 [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping]-[DEBUG] Mapping [/sync/init] to HandlerExecutionChain with handler [com.books.controller.SyncController@168eac0] and 1 interceptor
2011-10-12 17:28:35,954 [org.springframework.web.servlet.DispatcherServlet]-[DEBUG] Last-Modified value for [/test/sync/init] is: -1
2011-10-12 17:28:36,899 [org.springframework.jdbc.datasource.DataSourceTransactionManager]-[DEBUG] Creating new transaction with name [null]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
2011-10-12 17:28:36,899 [org.springframework.jdbc.datasource.DriverManagerDataSource]-[DEBUG] Creating new JDBC DriverManager Connection to [jdbc:mysql://localhost:3306/test]
2011-10-12 17:28:36,907 [org.springframework.jdbc.datasource.DataSourceTransactionManager]-[DEBUG] Acquired Connection [com.mysql.jdbc.JDBC4Connection@1317223] for JDBC transaction
2011-10-12 17:28:36,921 [org.springframework.jdbc.datasource.DataSourceTransactionManager]-[DEBUG] Switching JDBC Connection [com.mysql.jdbc.JDBC4Connection@1317223] to manual commit
2011-10-12 17:28:36,921 [org.mybatis.spring.SqlSessionUtils]-[DEBUG] Creating SqlSession with JDBC Connection [com.mysql.jdbc.JDBC4Connection@1317223]
2011-10-12 17:28:36,921 [java.sql.Connection]-[DEBUG] ooo Connection Opened
2011-10-12 17:28:36,921 [org.mybatis.spring.transaction.SpringManagedTransaction]-[DEBUG] JDBC Connection [com.mysql.jdbc.JDBC4Connection@1317223] will be managed by Spring
2011-10-12 17:28:36,921 [org.mybatis.spring.SqlSessionUtils]-[DEBUG] Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1cecdd7]
2011-10-12 17:28:36,929 [java.sql.PreparedStatement]-[DEBUG] ==> Executing: insert into users( id,nick, psw, is_sync, is_validate_psw, init_date,version_id) values( ?, ?, ?, ?, ?, ?, ?)
2011-10-12 17:28:36,929 [java.sql.PreparedStatement]-[DEBUG] ==> Parameters: 180012197(Long), test(String), null, false(Boolean), false(Boolean), 2011-10-12(Date), 0(Integer)
2011-10-12 17:28:36,930 [org.mybatis.spring.SqlSessionUtils]-[DEBUG] Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1cecdd7]
2011-10-12 17:28:36,930 [org.mybatis.spring.SqlSessionUtils]-[DEBUG] Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1cecdd7] from current transaction
2011-10-12 17:28:37,034 [java.sql.PreparedStatement]-[DEBUG] ==> Executing: insert into item (id, outer_id, title, props, cost, comments, owner ) values (?, ?, ?, ?, ?,?, ?, ?)
2011-10-12 17:28:37,035 [java.sql.PreparedStatement]-[DEBUG] ==> Parameters: 50329(Long), 一二三四五六七八九是一二三四五六七八九时一(String), 23(String), 20000:29527;20518:28416;20518:28417;20664:28105;20666:29937;20677:29952;35281:136847;1625899:3216971;1627207:28332;1627207:3232481;2915146:53908;2917721:3269957;2918248:3267690;(String), 12.5(Double), null, 180012197(Long)
2011-10-12 17:28:37,035 [org.mybatis.spring.SqlSessionUtils]-[DEBUG] Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1cecdd7]
2011-10-12 17:28:37,036 [org.springframework.jdbc.datasource.DataSourceTransactionManager]-[DEBUG] Initiating transaction rollback
2011-10-12 17:28:37,036 [org.springframework.jdbc.datasource.DataSourceTransactionManager]-[DEBUG] Rolling back JDBC transaction on Connection [com.mysql.jdbc.JDBC4Connection@1317223]
2011-10-12 17:28:37,065 [org.mybatis.spring.SqlSessionUtils]-[DEBUG] Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1cecdd7]
2011-10-12 17:28:37,065 [org.springframework.jdbc.datasource.DataSourceTransactionManager]-[DEBUG] Releasing JDBC Connection [com.mysql.jdbc.JDBC4Connection@1317223] after transaction
2011-10-12 17:28:37,065 [org.springframework.jdbc.datasource.DataSourceUtils]-[DEBUG] Returning JDBC Connection to DataSource
2011-10-12 17:28:37,065 [org.springframework.web.servlet.DispatcherServlet]-[DEBUG] Null ModelAndView returned to DispatcherServlet with name 'dispatcher': assuming HandlerAdapter completed request handling
2011-10-12 17:28:37,065 [org.springframework.web.servlet.DispatcherServlet]-[DEBUG] Successfully completed request
2011-10-12 17:28:37,066 [org.springframework.web.servlet.DispatcherServlet]-[DEBUG] Successfully completed request
[/quote]
@Inject
private DataSourceTransactionManager transactionManager;
方法中:
public void init(User user, Item item) throws Exception{
DefaultTransactionDefinition def = new DefaultTransactionDefinition();//事务定义类
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
TransactionStatus status = transactionManager.getTransaction(def);//返回事务对象
try {
dao.insert("userMapper.insert", user);
dao.insertList("itemMapper.insertList", items);
if(true) throw new Exception();
transactionManager.commit(status);
}catch (Exception ex) {
transactionManager.rollback(status);
}
}
能回滚,结果如下:
[quote]
2011-10-12 17:28:35,286 [org.springframework.jdbc.datasource.DataSourceUtils]-[DEBUG] Fetching JDBC Connection from DataSource
2011-10-12 17:28:35,286 [org.springframework.jdbc.datasource.DriverManagerDataSource]-[DEBUG] Creating new JDBC DriverManager Connection to [jdbc:mysql://localhost:3306/test]
2011-10-12 17:28:35,700 [org.mybatis.spring.SqlSessionUtils]-[DEBUG] Creating SqlSession with JDBC Connection [com.mysql.jdbc.JDBC4Connection@dd5f6d]
2011-10-12 17:28:35,706 [java.sql.Connection]-[DEBUG] ooo Connection Opened
2011-10-12 17:28:35,713 [org.mybatis.spring.transaction.SpringManagedTransaction]-[DEBUG] JDBC Connection [com.mysql.jdbc.JDBC4Connection@dd5f6d] will not be managed by Spring
2011-10-12 17:28:35,731 [org.mybatis.spring.SqlSessionUtils]-[DEBUG] SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@bfd849] was not registered for synchronization because synchronization is not active
2011-10-12 17:28:35,922 [java.sql.PreparedStatement]-[DEBUG] ==> Executing: select id, nick, psw, is_sync as isSync, is_validate_psw as isValidatePsw, init_date as initDate, version_id as versionId from users where nick = ?
2011-10-12 17:28:35,922 [java.sql.PreparedStatement]-[DEBUG] ==> Parameters: test(String)
2011-10-12 17:28:35,946 [org.mybatis.spring.SqlSessionUtils]-[DEBUG] Closing no transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@bfd849]
2011-10-12 17:28:35,948 [org.springframework.jdbc.datasource.DataSourceUtils]-[DEBUG] Returning JDBC Connection to DataSource
2011-10-12 17:28:35,953 [org.springframework.web.servlet.DispatcherServlet]-[DEBUG] Rendering view [org.springframework.web.servlet.view.InternalResourceView: unnamed; URL [init]] in DispatcherServlet with name 'dispatcher'
2011-10-12 17:28:35,953 [org.springframework.web.servlet.view.InternalResourceView]-[DEBUG] Forwarding to resource [init] in InternalResourceView 'null'
2011-10-12 17:28:35,954 [org.springframework.web.servlet.DispatcherServlet]-[DEBUG] DispatcherServlet with name 'dispatcher' processing GET request for [/test/sync/init]
2011-10-12 17:28:35,954 [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping]-[DEBUG] Mapping [/sync/init] to HandlerExecutionChain with handler [com.books.controller.SyncController@168eac0] and 1 interceptor
2011-10-12 17:28:35,954 [org.springframework.web.servlet.DispatcherServlet]-[DEBUG] Last-Modified value for [/test/sync/init] is: -1
2011-10-12 17:28:36,899 [org.springframework.jdbc.datasource.DataSourceTransactionManager]-[DEBUG] Creating new transaction with name [null]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
2011-10-12 17:28:36,899 [org.springframework.jdbc.datasource.DriverManagerDataSource]-[DEBUG] Creating new JDBC DriverManager Connection to [jdbc:mysql://localhost:3306/test]
2011-10-12 17:28:36,907 [org.springframework.jdbc.datasource.DataSourceTransactionManager]-[DEBUG] Acquired Connection [com.mysql.jdbc.JDBC4Connection@1317223] for JDBC transaction
2011-10-12 17:28:36,921 [org.springframework.jdbc.datasource.DataSourceTransactionManager]-[DEBUG] Switching JDBC Connection [com.mysql.jdbc.JDBC4Connection@1317223] to manual commit
2011-10-12 17:28:36,921 [org.mybatis.spring.SqlSessionUtils]-[DEBUG] Creating SqlSession with JDBC Connection [com.mysql.jdbc.JDBC4Connection@1317223]
2011-10-12 17:28:36,921 [java.sql.Connection]-[DEBUG] ooo Connection Opened
2011-10-12 17:28:36,921 [org.mybatis.spring.transaction.SpringManagedTransaction]-[DEBUG] JDBC Connection [com.mysql.jdbc.JDBC4Connection@1317223] will be managed by Spring
2011-10-12 17:28:36,921 [org.mybatis.spring.SqlSessionUtils]-[DEBUG] Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1cecdd7]
2011-10-12 17:28:36,929 [java.sql.PreparedStatement]-[DEBUG] ==> Executing: insert into users( id,nick, psw, is_sync, is_validate_psw, init_date,version_id) values( ?, ?, ?, ?, ?, ?, ?)
2011-10-12 17:28:36,929 [java.sql.PreparedStatement]-[DEBUG] ==> Parameters: 180012197(Long), test(String), null, false(Boolean), false(Boolean), 2011-10-12(Date), 0(Integer)
2011-10-12 17:28:36,930 [org.mybatis.spring.SqlSessionUtils]-[DEBUG] Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1cecdd7]
2011-10-12 17:28:36,930 [org.mybatis.spring.SqlSessionUtils]-[DEBUG] Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1cecdd7] from current transaction
2011-10-12 17:28:37,034 [java.sql.PreparedStatement]-[DEBUG] ==> Executing: insert into item (id, outer_id, title, props, cost, comments, owner ) values (?, ?, ?, ?, ?,?, ?, ?)
2011-10-12 17:28:37,035 [java.sql.PreparedStatement]-[DEBUG] ==> Parameters: 50329(Long), 一二三四五六七八九是一二三四五六七八九时一(String), 23(String), 20000:29527;20518:28416;20518:28417;20664:28105;20666:29937;20677:29952;35281:136847;1625899:3216971;1627207:28332;1627207:3232481;2915146:53908;2917721:3269957;2918248:3267690;(String), 12.5(Double), null, 180012197(Long)
2011-10-12 17:28:37,035 [org.mybatis.spring.SqlSessionUtils]-[DEBUG] Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1cecdd7]
2011-10-12 17:28:37,036 [org.springframework.jdbc.datasource.DataSourceTransactionManager]-[DEBUG] Initiating transaction rollback
2011-10-12 17:28:37,036 [org.springframework.jdbc.datasource.DataSourceTransactionManager]-[DEBUG] Rolling back JDBC transaction on Connection [com.mysql.jdbc.JDBC4Connection@1317223]
2011-10-12 17:28:37,065 [org.mybatis.spring.SqlSessionUtils]-[DEBUG] Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1cecdd7]
2011-10-12 17:28:37,065 [org.springframework.jdbc.datasource.DataSourceTransactionManager]-[DEBUG] Releasing JDBC Connection [com.mysql.jdbc.JDBC4Connection@1317223] after transaction
2011-10-12 17:28:37,065 [org.springframework.jdbc.datasource.DataSourceUtils]-[DEBUG] Returning JDBC Connection to DataSource
2011-10-12 17:28:37,065 [org.springframework.web.servlet.DispatcherServlet]-[DEBUG] Null ModelAndView returned to DispatcherServlet with name 'dispatcher': assuming HandlerAdapter completed request handling
2011-10-12 17:28:37,065 [org.springframework.web.servlet.DispatcherServlet]-[DEBUG] Successfully completed request
2011-10-12 17:28:37,066 [org.springframework.web.servlet.DispatcherServlet]-[DEBUG] Successfully completed request
[/quote]