Spring配置声明式事务,在service的方法save*方法中同时用到hibernateTemplate、jdbcTemplate操作数据库,相同配置下在sqlServer可以保证事务,数据库换成MySql后用jdbc进行的操作不能回滚,初步怀疑是MySql的autocommit属性未设置好,劳烦高手看下~主要配置如下:
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://10.40.207.33:3306/cdp?useUnicode=true&characterEncoding=UTF-8">
</property>
<property name="username" value="mysql"></property>
<property name="password" value="zxumamysql"></property>
<property name="initialSize" value="5" />
<property name="maxActive" value="100" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"></ref>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.query.factory_class">
org.hibernate.hql.classic.ClassicQueryTranslatorFactory
</prop>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.query.factory_class">
org.hibernate.hql.ast.ASTQueryTranslatorFactory
</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.cache.use_second_level_cache">
false
</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
<prop key="hibernate.connection.autocommit">false</prop>
</props>
</property>
<property name="mappingResources">
<list>
</list>
</property>
</bean>
<!-- 事务管理 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!-- 用户代理 -->
<bean id="currencyServiceimpl"
class="com.wootion.cdp.services.impl.CurrencyServiceImpl">
<property name="daoTool">
<ref bean="daoTool" />
</property>
</bean>
<bean id="icurrencyService"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager" />
</property>
<property name="target">
<ref local="currencyServiceimpl" />
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean> <!-- 添加对jdbc的支持 begin -->
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<bean id="jdbcDaoTool"
class="com.wootion.workflow.common.idao.impl.JdbcDaoToolImpl">
<property name="jdbcTemplate">
<ref bean="jdbcTemplate" />
</property>
</bean>
<!-- 添加对jdbc的支持 end -->
<bean id="jdbcServiceImpl" parent="icurrencyService" >
<property name="target">
<bean
class="com.wootion.workflow.common.services.impl.JdbcServiceImpl">
<property name="jdbcDaoTool">
<ref bean="jdbcDaoTool" />
</property>
</bean>
</property>
</bean>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://10.40.207.33:3306/cdp?useUnicode=true&characterEncoding=UTF-8">
</property>
<property name="username" value="mysql"></property>
<property name="password" value="zxumamysql"></property>
<property name="initialSize" value="5" />
<property name="maxActive" value="100" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"></ref>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.query.factory_class">
org.hibernate.hql.classic.ClassicQueryTranslatorFactory
</prop>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.query.factory_class">
org.hibernate.hql.ast.ASTQueryTranslatorFactory
</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.cache.use_second_level_cache">
false
</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
<prop key="hibernate.connection.autocommit">false</prop>
</props>
</property>
<property name="mappingResources">
<list>
</list>
</property>
</bean>
<!-- 事务管理 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!-- 用户代理 -->
<bean id="currencyServiceimpl"
class="com.wootion.cdp.services.impl.CurrencyServiceImpl">
<property name="daoTool">
<ref bean="daoTool" />
</property>
</bean>
<bean id="icurrencyService"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager" />
</property>
<property name="target">
<ref local="currencyServiceimpl" />
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean> <!-- 添加对jdbc的支持 begin -->
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<bean id="jdbcDaoTool"
class="com.wootion.workflow.common.idao.impl.JdbcDaoToolImpl">
<property name="jdbcTemplate">
<ref bean="jdbcTemplate" />
</property>
</bean>
<!-- 添加对jdbc的支持 end -->
<bean id="jdbcServiceImpl" parent="icurrencyService" >
<property name="target">
<bean
class="com.wootion.workflow.common.services.impl.JdbcServiceImpl">
<property name="jdbcDaoTool">
<ref bean="jdbcDaoTool" />
</property>
</bean>
</property>
</bean>
解决方案 »
- Parameter(参数) 和Attribute(属性)的区别
- Hibernate连接access问题
- 浅析:Java栈与堆的区别(一)
- Spring Hibernate
- 发个自己要用的东西,
- Struts <logic:iterial> 標簽問題
- 哪里可以得到jxl.jar?谁那里有jxl输出excel的例子?(急!!!!!!!!!!!!!)
- hibernate的annotation疑问~?
- win98 跑jsp resin2.0.1+mysql装好了,写好的代码放上去,在地址栏敲http://localhost/
- 我进入IT业1年,我所在的公司只有一个美工和我两个技术人员,虽然什么项目都要我做,但是感觉是在自学,目光短浅,请问我是继续在这个公
- Hibernate 本地SQL查询 HQL查询
- 一个有关js的思路
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd"><beans>
<!-- 连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/godob</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>86967518</value>
</property>
</bean> <!-- 连接池 -->
<bean id="dataSourceByInterim" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/gododb</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>86967518</value>
</property>
</bean>
<!-- ========================= JDBC DataSource Definitions ========================= -->
<!--
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@172.16.115.192:1521:bdms</value>
</property>
<property name="username">
<value>dmsdcad</value>
</property>
<property name="password">
<value>dmsdcad</value>
</property>
<property name="maxActive">
<value>100</value>
</property>
<property name="maxIdle">
<value>100</value>
</property>
<property name="minIdle">
<value>1</value>
</property>
<property name="initialSize">
<value>1</value>
</property>
<property name="maxWait">
<value>5000</value>
</property>
</bean>
-->
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> <!-- 定义事物管理器 -->
<bean id="myTransaction"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 定义事物规则 -->
<tx:advice id="txAdivice" transaction-manager="myTransaction">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="get*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="find*" propagation="REQUIRED" />
<tx:method name="do*" propagation="REQUIRED" />
<tx:method name="is*" propagation="REQUIRED" />
<!-- <tx:method name="export*" propagation="REQUIRED" /> -->
</tx:attributes>
</tx:advice> <!-- 定义切面, phoenix.biz.impl下所有类的所有方法都应用事物规则-->
<aop:config>
<aop:pointcut id="bizMethods" expression="execution(* com.phoenix.service.*.*.*(..))" />
<aop:advisor advice-ref="txAdivice" pointcut-ref="bizMethods" />
</aop:config>
<aop:config>
<aop:pointcut id="bizMethodsByInterim" expression="execution(* interim.service.*.*.*(..))" />
<aop:advisor advice-ref="txAdivice" pointcut-ref="bizMethodsByInterim" />
</aop:config>
</beans>
这是我直接做的一个SSH + mysql的项目配置,希望对你有帮助
也是使用的mysql
<property name="target">
<ref local="currencyServiceimpl" />
</property>
也没有引用jdbcServiceImpl呀,应该是没被代理吧
现在问题关键是:业务类的方法同时包含hibernate、jdbc对数据库的操作,相同的配置,用SqlServer事务正常,用MySql时jdbc的操作不能回滚~怀疑是对MySql的配置问题
org.hibernate.dialect.MySQLDialect
</prop> 改成<prop key="dialect">
org.hibernate.dialect.MySQLDialect
</prop>
也是aop的
好像是在tx:method的里面,加上rollback-for="Exception"就行了手头没代码,可能记错了也说不定。lz试试吧good luck
@guoyang100 表类型设置为InnoDB
@villagehead 不是异常类型的问题,测试时显式抛RuntimeException(默认)都不回滚
不需要JTA事务,本配置在SqlServer数据库下事务测试通过,换到MySql上出现贴中描述问题