spring 事务管理配置-ibatis
xml配置
Xml代码
1.<!-- 默认的数据源配置 -->
2.<bean id="talent.defaultDataSource"
3. />
6. <property name="url" value="${jdbc.default.url}" />
7. <property name="username" value="${jdbc.default.username}" />
8. <property name="password" value="${jdbc.default.password}" />
9.</bean>
10.
11.<!-- 事务配置 -->
12.<bean id="talent.defaultTransactionManager"
13. ref="talent.defaultDataSource" />
15.</bean>
16.
17.<!-- 配置事务特性 -->
18.<tx:advice id="txAdvice"
19. transaction-manager="talent.defaultTransactionManager">
20. <tx:attributes>
21. <tx:method name="add*" propagation="REQUIRED" />
22. <tx:method name="save*" propagation="REQUIRED" />
23. <tx:method name="insert*" propagation="REQUIRED" />
24. <tx:method name="del*" propagation="REQUIRED" />
25. <tx:method name="update*" propagation="REQUIRED" />
26. <tx:method name="main*" propagation="REQUIRED" />
27. <tx:method name="*" read-only="true" />
28. </tx:attributes>
29.</tx:advice>
30.
31.<!-- 配置哪些类的方法需要进行事务管理 -->
32.<aop:config>
33. <aop:pointcut id="allManagerMethod"
34. expression="execution(* com.jstrd.talent.manager.MyTransactionTemplate.*(..))" />
35. <aop:advisor advice-ref="txAdvice"
36. pointcut-ref="allManagerMethod" />
37.</aop:config>
38.
39.<!-- 默认的DaoFactory -->
40.<bean id="talent.defaultDaoFactory"
41. />
43.</bean>
44.
45.<!-- 默认的SqlMapClient -->
46.<bean id="talent.defaultSqlMapClient"
47. ref="talent.defaultDataSource" />
49. <property name="configLocation"
50. value="classpath:talent/ibatis/sql-map-config.xml" />
51. <!-- 自动加载sql-mapping文件 -->
52. <property name="mappingLocations">
53. <value>${ibatis.mappingLocations}</value>
54. </property>
55. <property name="useTransactionAwareDataSource" value="true"></property>
56.</bean>
57.
58.<!-- 默认的SqlMapDao -->
59.<bean id="talent.defaultSqlMapDao"
60. ref="talent.defaultSqlMapClient" />
62.</bean>
63.
64.<!-- 默认的SqlMapClientTemplate -->
65.<bean id="talent.defaultSqlMapClientTemplate"
66. ref="talent.defaultSqlMapClient" />
68.</bean>
<!-- 默认的数据源配置 -->
<bean id="talent.defaultDataSource"
/>
<property name="url" value="${jdbc.default.url}" />
<property name="username" value="${jdbc.default.username}" />
<property name="password" value="${jdbc.default.password}" />
</bean>
<!-- 事务配置 -->
<bean id="talent.defaultTransactionManager"
ref="talent.defaultDataSource" />
</bean>
<!-- 配置事务特性 -->
<tx:advice id="txAdvice"
transaction-manager="talent.defaultTransactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="main*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 配置哪些类的方法需要进行事务管理 -->
<aop:config>
<aop:pointcut id="allManagerMethod"
expression="execution(* com.jstrd.talent.manager.MyTransactionTemplate.*(..))" />
<aop:advisor advice-ref="txAdvice"
pointcut-ref="allManagerMethod" />
</aop:config>
<!-- 默认的DaoFactory -->
<bean id="talent.defaultDaoFactory"
/>
</bean>
<!-- 默认的SqlMapClient -->
<bean id="talent.defaultSqlMapClient"
ref="talent.defaultDataSource" />
<property name="configLocation"
value="classpath:talent/ibatis/sql-map-config.xml" />
<!-- 自动加载sql-mapping文件 -->
<property name="mappingLocations">
<value>${ibatis.mappingLocations}</value>
</property>
<property name="useTransactionAwareDataSource" value="true"></property>
</bean>
<!-- 默认的SqlMapDao -->
<bean id="talent.defaultSqlMapDao"
ref="talent.defaultSqlMapClient" />
</bean>
<!-- 默认的SqlMapClientTemplate -->
<bean id="talent.defaultSqlMapClientTemplate"
ref="talent.defaultSqlMapClient" />
</bean>
java代码
Java代码
1.public class MyTransactionTemplate {
2. public void addXX() throws Exception {
3. SqlMapDao dao = DaoFactory.getSqlMapDao();
4. SqlMapClientTemplate sqlMap = dao.getSqlMapClientTemplate();//dao.getSqlMapClientTemplate();//(SqlMapClientTemplate)BeanFactory.getBean("talent.defaultSqlMapClientTemplate");
5. sqlMap.update("t_user.delete");
6. sqlMap.update("t_user.insert", new TUser(29, "tan29"));
7. sqlMap.update("t_user.insert", new TUser(30, "tan30"));
8. sqlMap.update("t_user.insert", new TUser(32, "tan32"));
9.
10. sqlMap.update("t_user.updateById", new TUser(29, "tan30"));//这里违反唯一约束,会抛异常的
11. sqlMap.update("t_user.updateById", new TUser(29, "tan28"));
12. }
13.}
public class MyTransactionTemplate {
public void addXX() throws Exception {
SqlMapDao dao = DaoFactory.getSqlMapDao();
SqlMapClientTemplate sqlMap = dao.getSqlMapClientTemplate();//dao.getSqlMapClientTemplate();//(SqlMapClientTemplate)BeanFactory.getBean("talent.defaultSqlMapClientTemplate");
sqlMap.update("t_user.delete");
sqlMap.update("t_user.insert", new TUser(29, "tan29"));
sqlMap.update("t_user.insert", new TUser(30, "tan30"));
sqlMap.update("t_user.insert", new TUser(32, "tan32"));
sqlMap.update("t_user.updateById", new TUser(29, "tan30"));//这里违反唯一约束,会抛异常的
sqlMap.update("t_user.updateById", new TUser(29, "tan28"));
}
}
java代码中,连续几个insert,然后两上update,其中一个update是会抛异常的。但是发现前面的insert已经在数据库中生效了,并不回滚
问题补充:
数据库是mysql5
"MyTransactionTemplate根本没有处于事务中啊"??已经配置在其中了的:
execution(* com.jstrd.talent.manager.MyTransactionTemplate.*(..))"
问题补充:
谢谢kyo100900的回答!我改成InnoDb后,事务还是没控制住,
日志发现sqlMap.update()每次都会
Fetching JDBC Connection from DataSource
然后
Returning JDBC Connection to DataSource
这里可能也有问题?!
问题补充:
换了oracle环境还是一样,我怀疑是配错了
问题补充:
xml配置
<pre name="code"
value="${jdbc.default.driverClassName}" />
<property name="url" value="${jdbc.default.url}" />
<property name="username" value="${jdbc.default.username}" />
<property name="password" value="${jdbc.default.password}" />
</bean>
<!-- 事务配置 -->
<bean id="talent.defaultTransactionManager"
ref="talent.defaultDataSource" />
</bean>
<!-- 配置事务特性 -->
<tx:advice id="txAdvice"
transaction-manager="talent.defaultTransactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="main*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 配置哪些类的方法需要进行事务管理 -->
<aop:config>
<aop:pointcut id="allManagerMethod"
expression="execution(* com.jstrd.talent.manager.MyTransactionTemplate.*(..))" />
<aop:advisor advice-ref="txAdvice"
pointcut-ref="allManagerMethod" />
</aop:config>
<!-- 默认的DaoFactory -->
<bean id="talent.defaultDaoFactory"
/>
</bean>
<!-- 默认的SqlMapClient -->
<bean id="talent.defaultSqlMapClient"
ref="talent.defaultDataSource" />
<property name="configLocation"
value="classpath:talent/ibatis/sql-map-config.xml" />
<!-- 自动加载sql-mapping文件 -->
<property name="mappingLocations">
<value>${ibatis.mappingLocations}</value>
</property>
<property name="useTransactionAwareDataSource" value="true"></property>
</bean>
<!-- 默认的SqlMapDao -->
<bean id="talent.defaultSqlMapDao"
ref="talent.defaultSqlMapClient" />
</bean>
<!-- 默认的SqlMapClientTemplate -->
<bean id="talent.defaultSqlMapClientTemplate"
ref="talent.defaultSqlMapClient" />
</bean>
</pre>
java代码
<pre name="code"
<strong>问题补充:</strong>
谢谢kyo100900的回答!我改成InnoDb后,事务还是没控制住,
日志发现sqlMap.update()每次都会
Fetching JDBC Connection from DataSource
然后
Returning JDBC Connection to DataSource
这里可能也有问题?!
<strong>问题补充:</strong>
换了oracle环境还是一样,我怀疑是配错了
调试发现SqlMapClientTemplate的事务管理器为com.ibatis.sqlmap.engine.transaction.TransactionManager@19f9c7a.跟配置的不一样啊
问题补充:
"MyTransactionTemplate根本没有处于事务中啊"??已经配置在其中了的:
execution(* com.jstrd.talent.manager.MyTransactionTemplate.*(..))"
<strong>问题补充:</strong>
谢谢kyo100900的回答!我改成InnoDb后,事务还是没控制住,
日志发现sqlMap.update()每次都会
Fetching JDBC Connection from DataSource
然后
Returning JDBC Connection to DataSource
这里可能也有问题?!
<strong>问题补充:</strong>
换了oracle环境还是一样,我怀疑是配错了
调试发现SqlMapClientTemplate的事务管理器为com.ibatis.sqlmap.engine.transaction.TransactionManager@19f9c7a.跟配置的不一样啊