Spring 3 + Hibernate4 事务不成功
本帖最后由 tangtangzizi 于 2013-05-22 23:54:23 编辑 先贴配置吧:
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="username" value="root" />
<property name="password" value="123" />
<property name="url" value="jdbc:mysql://localhost:3306/FindClass" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
<property name="dataSource" >
<ref local="dataSource"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<!--
<prop key="javax.persistence.validation.mode">none</prop>
-->
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
<prop key="hibernate.connection.autocommit">false</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>funs.el.mvc.model</value>
</list>
</property>
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="list*" propagation="REQUIRED" read-only="true" />
<tx:method name="load*" propagation="REQUIRED" read-only="true" />
<tx:method name="save*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="add*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="save" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="update*" propagation="REQUIRED"
rollback-for="Exception" />
<tx:method name="update" propagation="REQUIRED"
rollback-for="Exception" />
<tx:method name="delete*" propagation="REQUIRED"
rollback-for="Exception" />
<tx:method name="delete" propagation="REQUIRED"
rollback-for="Exception" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:aspectj-autoproxy />
<aop:config>
<aop:pointcut id="aopPointcut"
expression="execution(* funs.el.mvc.service..*.*ServiceImpl.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="aopPointcut"/>
</aop:config>
</beans>
我再整合spring3+Hibernate4,里面的ORM和spring的 bean都是用注解的方式配置的,事务我也想用注解的方式配置,但怎么配置也配置不对,上面就是我配置事务的代码(貌似直接加个@Transactional注解就可以直接写上面的tx:annotation-driven就可以了吧?)。但事实上不行。
其一,我目的是当整个service方法抛出异常了,事务代理自动帮我回滚,但我测试过不行。
其二,我将所有注释的和配置形式的事务代理配置都删掉了,结果我随便一个带修改的操作也成功将数据库修改了(按道理我没配置开启事务的话,是不是不应该自动提交的呢?)。
我已经在配置里显示的写了:<prop key="hibernate.connection.autocommit">false</prop>
貌似hibernate默认就是不开启自动提交的。
另外我附上一段DAO的插入代码:
@Overridespring Hibernate 事务 异常 Java
public boolean add(T t) {
// TODO Auto-generated method stub
sessionFactory.getCurrentSession().save(t);
return true;
}
[解决办法]
另外。
@EnableTransactionManagement and <tx:annotation-driven/> only looks for @Transactional on beans in the same application context they are defined in. This means that, if you put annotation driven configuration in a WebApplicationContext for a DispatcherServlet, it only checks for @Transactional beans in your controllers, and not your services. See Section 17.2, “The DispatcherServlet” for more information.
这是官网的提示。
它查找application context they are defined in 。并且不能定义到DispatcherServlet
所以包扫描必不可少