spring事务配置在annotation不启作用,请问有什么解决办法或其它配置方法3
2016-10-13 13:49
537 查看
环境:
spring版本2.5.6
Hibernate 3.3.1.GA
使用openSessionInView
spring事务配置两种对使用XML都有效.
AOP配置如下:
Java代码
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<aop:aspectj-autoproxy proxy-target-class="true"/>
<aop:config>
<aop:advisor pointcut="execution(* com.iit..service*..*(..))" advice-ref="txAdvice" order="1"/>
<aop:advisor pointcut="execution(* com.iit..plugin*..*(..))" advice-ref="txAdvice" order="2"/>
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" rollback-for="Throwable" />
<tx:method name="load*" read-only="true" rollback-for="Throwable" />
<tx:method name="find*" read-only="true" rollback-for="Throwable" />
<tx:method name="*" rollback-for="Throwable" />
</tx:attributes>
</tx:advice>
transationInterceptor配置如下:
Java代码
<bean id="transationInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="Save*">PROPAGATION_REQUIRED</prop>
<prop key="merge*">PROPAGATION_REQUIRED</prop>
<prop key="shield*" >PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<value>*ManagerImpl,*Dao,*Service,*ServiceImpl</value>
</property>
<property name="interceptorNames">
<list>
<value>transationInterceptor</value>
</list>
</property>
</bean>
当service class使用XML配置时,事务是有效
Java代码
<bean id="testTxService" class="com.iit.test.service.impl.TestTxServiceImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
当使用annotation配置时,事务无效,不会生成代理.
Java代码
@Service("testTxManager")
public class TestTxManagerImpl implements TestTxManager {
private SimpleHibernateDao<TestTx, Long> testTxDao;
@Required
@Autowired
public void setSessionFactory(@Qualifier("sessionFactory")
SessionFactory sessionFactory) {
testTxDao = new SimpleHibernateDao<TestTx, Long>(sessionFactory, TestTx.class);
}
public void saveTest() {
for (int i = 0; i < 3; i++) {
TestTx testTx = new TestTx();
testTx.setTxName("Kevin" + i);
testTxDao.merge(testTx);
if (i == 2) {
throw new RuntimeException("test");
// int k = 1/0;
}
}
}
}
且报下面错误:
Java代码
org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
请问怎么配置才能让事务在annotation生效?
问题补充:
使用cwx714的配置:
Java代码
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
有某此情况下是可以的(如下配置).但service还是需要写在XML文件中.
Java代码
<bean id="TestTxManager" class="com.iit.test.service.impl.TestTxManagerImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
Java代码
<!-- ①:对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能-->
<context:component-scan base-package="com.iit.test.webapp.controller"/>
请注意:一定要指定到controller这个目录下,如:com.iit.test.webapp.controller,而不能是整个com.iit
这个配置在spring2.5的petclinic例子下,也是这样配置.
petclinic的配置是这样的:
Java代码
<tx:annotation-driven />
<!-- PetClinic's central data access object: Hibernate implementation-->
<bean id="clinic" class="org.springframework.samples.petclinic.hibernate.HibernateClinic"/>
Java代码
<context:component-scan base-package="org.springframework.samples.petclinic.web" />
为什么service要写在XML中呢?
为什么扫描的包不能扩大呢?
问题补充:
我找呀,找呀,终于找到答案了.
事务配置成功.
1.<context:component-scan base-package="com.iit.webapp"/>只能针对Web层,
Service,DAO层不能包含在里面.
如果有多个package,使用","分隔即可.
2.在XML中需要声明service的名字与class,引用可以利用@Autowired注解来简化依赖注入.
0spring版本2.5.6
Hibernate 3.3.1.GA
使用openSessionInView0
在applicationContext.xml中加入:
Java代码
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
在业务对象前加入:
Java代码
@Service
@Transactional
public class TestTxManagerImpl {
...
}
spring版本2.5.6
Hibernate 3.3.1.GA
使用openSessionInView
spring事务配置两种对使用XML都有效.
AOP配置如下:
Java代码
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<aop:aspectj-autoproxy proxy-target-class="true"/>
<aop:config>
<aop:advisor pointcut="execution(* com.iit..service*..*(..))" advice-ref="txAdvice" order="1"/>
<aop:advisor pointcut="execution(* com.iit..plugin*..*(..))" advice-ref="txAdvice" order="2"/>
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" rollback-for="Throwable" />
<tx:method name="load*" read-only="true" rollback-for="Throwable" />
<tx:method name="find*" read-only="true" rollback-for="Throwable" />
<tx:method name="*" rollback-for="Throwable" />
</tx:attributes>
</tx:advice>
transationInterceptor配置如下:
Java代码
<bean id="transationInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="Save*">PROPAGATION_REQUIRED</prop>
<prop key="merge*">PROPAGATION_REQUIRED</prop>
<prop key="shield*" >PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<value>*ManagerImpl,*Dao,*Service,*ServiceImpl</value>
</property>
<property name="interceptorNames">
<list>
<value>transationInterceptor</value>
</list>
</property>
</bean>
当service class使用XML配置时,事务是有效
Java代码
<bean id="testTxService" class="com.iit.test.service.impl.TestTxServiceImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
当使用annotation配置时,事务无效,不会生成代理.
Java代码
@Service("testTxManager")
public class TestTxManagerImpl implements TestTxManager {
private SimpleHibernateDao<TestTx, Long> testTxDao;
@Required
@Autowired
public void setSessionFactory(@Qualifier("sessionFactory")
SessionFactory sessionFactory) {
testTxDao = new SimpleHibernateDao<TestTx, Long>(sessionFactory, TestTx.class);
}
public void saveTest() {
for (int i = 0; i < 3; i++) {
TestTx testTx = new TestTx();
testTx.setTxName("Kevin" + i);
testTxDao.merge(testTx);
if (i == 2) {
throw new RuntimeException("test");
// int k = 1/0;
}
}
}
}
且报下面错误:
Java代码
org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
请问怎么配置才能让事务在annotation生效?
问题补充:
使用cwx714的配置:
Java代码
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
有某此情况下是可以的(如下配置).但service还是需要写在XML文件中.
Java代码
<bean id="TestTxManager" class="com.iit.test.service.impl.TestTxManagerImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
Java代码
<!-- ①:对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能-->
<context:component-scan base-package="com.iit.test.webapp.controller"/>
请注意:一定要指定到controller这个目录下,如:com.iit.test.webapp.controller,而不能是整个com.iit
这个配置在spring2.5的petclinic例子下,也是这样配置.
petclinic的配置是这样的:
Java代码
<tx:annotation-driven />
<!-- PetClinic's central data access object: Hibernate implementation-->
<bean id="clinic" class="org.springframework.samples.petclinic.hibernate.HibernateClinic"/>
Java代码
<context:component-scan base-package="org.springframework.samples.petclinic.web" />
为什么service要写在XML中呢?
为什么扫描的包不能扩大呢?
问题补充:
我找呀,找呀,终于找到答案了.
事务配置成功.
1.<context:component-scan base-package="com.iit.webapp"/>只能针对Web层,
Service,DAO层不能包含在里面.
如果有多个package,使用","分隔即可.
2.在XML中需要声明service的名字与class,引用可以利用@Autowired注解来简化依赖注入.
按时间排序按投票排序
0spring版本2.5.6 Hibernate 3.3.1.GA
使用openSessionInView0
在applicationContext.xml中加入:
Java代码
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
在业务对象前加入:
Java代码
@Service
@Transactional
public class TestTxManagerImpl {
...
}
相关文章推荐
- spring @Transactional 方法内事务不起作用的解决办法
- 关于spring4和hibernate4整合,配置事务报“Cannot unwrap to requested type [javax.sql.DataSource]”错误的解决方法及心得
- Spring+SpringMVC配置事务管理无效原因及解决办法详解
- 由OpenSessionInViewFilter报错引出的spring3.0事务配置问题及解决办法
- spring 在有事务的方法上配置自定义日志切面,事务会失效解决方法
- 由OpenSessionInViewFilter报错引出的spring3.0事务配置问题及解决办法
- struts hibernate spring 环境配置时出现 HTTP Status 404 Servlet action is not available (解决方法)
- spring 事务配置 的一种方法
- Spring 中基础配置和事务管理的配置和spring托管hibernate配置文件的方法
- 关掉\禁用win7自动配置ipv4地址的方法 默认网关自动消失的解决办法
- hibernate3.0+ejb3 annotaion配置实战+spring1.21 annotation事务控制
- Spring的事务配置方法
- 用spring管理hibernate事务时,lzay="true"不能用的解决方法
- Dwr2+Log4j+Struts1.2+Spring2.0+Hibernate3.1_in_MyEclipse5.5配置问题及解决方法
- Spring Ibatis父子容器事务失效解决办法
- spring3.0 MVC 继承SimpleFormController不执行onSubmit方法解决办法。
- Spring的事务配置方法
- spring五种配置事务方法
- spring事务配置方法(BeanNameAutoProxyCreator)
- Spring事务配置的五种方法