遇到了一个spring配置事务不能生效的问题,在网上查了一下各种解决方案,但还是不能解决,不知道问题根源在哪里,请各位大神指点,非常感谢。
<bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
<property name="driver">
......
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="packagesToScan">
<list>
<value>com.abc</value>
</list>
</property>
......
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="mySessionFactory"></property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="mySessionFactory" />
</property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" />
<tx:method name="*" read-only="true" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="serviceMethod" expression="execution(* com.abc.*.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
</aop:config>
不知道是不是配置的问题,代码采用的是springMVC+spring+hibernate的框架,采用的是注解方式,service和DAO中都使用过注解定义事务,仍然没有作用,数据操作采用的是getHibernateTemplate().save...。
------解决方案--------------------
你不是基于注解的吗,这是基于注解的方式使用事务配置声明,试试
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
<!-- enable autowire -->
<context:annotation-config />
------解决方案--------------------
你采用哪种事务配置方式,注解还是AOP方式?
------解决方案--------------------
http://www.blogjava.net/robbie/archive/2009/04/05/264003.html楼主参考一下
------解决方案--------------------
把service里的代码也发点出来啊,
------解决方案--------------------
我怀疑是你的类名起错了(包括包名),配置文件没什么大问题,就算你基于注解但是你的事务是不需要基于注解的。
------解决方案--------------------
你是所有带有save前缀的方法都会使用事务,看看你的方法是否正确
------解决方案--------------------
expression="execution(* com.abc.*.service.*.*(..))" 指定的切点是否正确? 只有执行了这里aop切入的方法,spring才会开起事务。你aop已经配了,注解是多余的。
你可以断点到TransactionInterceptor的invoke方法里面,验证你的方法是否被aop切入事务。
还有如果被aop Interceptor拦截到的话, eclipse(我不确定你的有没有,我用的sts有)是可以在方法的行号旁边看aop拦截标识的。 一个类似于"("的符号。
Multiple markers at this line
- implements com.xxxx.xxx.xxxService.xxxx
- advised by
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(org.aopalliance.intercept.MethodInvocation)
鼠标放到符号上还有上面的提示
------解决方案--------------------
spring会对事务做管理,里面规定了什么样的方法调用它会经过事务的管理,比如你配置的<tx:method name="update*" propagation="REQUIRED"/>,这个表明你调用action中调用update的方法名字必须为update*(),这样它才会纳入事务的管理,才会commit