问题描述
业务bean:@Transactionalpublic class PersonServiceBean implements PersonService {@Resource private SessionFactory sessionFactory;public void save(Person person){sessionFactory.getCurrentSession().persist(person); session,persist()}public void update(Person person){sessionFactory.getCurrentSession().merge(person);}@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)public Person getPerson(Integer personId){return (Person)sessionFactory.getCurrentSession().get(Person.class, personId);}public void delete(Integer personId){sessionFactory.getCurrentSession().delete(sessionFactory.getCurrentSession().load(Person.class, personId));}}spring配置文件:<aop:aspectj-autoproxy /> <context:property-placeholder location="classpath:jdbc.properties"/> <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${driveClassName}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> <property name="initialSize" value="${initialSize}"/> <property name="maxActive" value="${maxActive}"/> <property name="maxIdle" value="${maxIdle}"/> <property name="minIdle" value="${minIdle}"/> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="myDataSource"/> <property name="mappingResources"> <list> <value>com/tank/bean/Person.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">false</prop> <prop key="hibernate.cache.use_second_level_cache">true</prop><prop key="hibernate.cache.use_query_cache">false</prop><prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> </props> </property> </bean> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <tx:annotation-driven transaction-manager="txManager"/><aop:config><aop:pointcut id="myPointCut" expression="execution(* com.tank.service..*.*(..))" /><aop:advisor pointcut-ref="myPointCut" advice-ref="txAdvice"/></aop:config><tx:advice id="txAdvice" transaction-manager="txManager"><tx:attributes> <tx:method name="get*" read-only="true"/> <tx:method name="delete*" /> <tx:method name="save*" read-only="true"/> <tx:method name="update*" /> <tx:method name="*" propagation="SUPPORTS"/> </tx:attributes></tx:advice> <bean id="personService" class="com.tank.service.impl.PersonServiceBean"/> <bean id="personList" class="com.tank.web.PersonAction.PersonAction"/>问题: <tx:method name="save*" read-only="true"/> 这个方法已经为只读,但仍能保存进数据库业务bean已经被@Transactional了。 如果我在save()方法上在注明@Transactional(propagation=Propagation.SUPPORTES)的话,可以生效!!什么原因?貌似管理也有啊!!!
解决方案
大哥,你又用@Transactional注解事物,又用aop的expression正则配置方法事务。两者已经冲突了。 @Transactional在类当中配置全局事务,已经覆盖了你xml文件配置的声明式事物。去掉类当中的@Transactional就ok了。
解决方案二:
<tx:annotation-driven transaction-manager="txManager"/> 就不需要<tx:method name="save*" read-only="true"/> 这个了! 所以引用<tx:method name="save*" read-only="true"/> 这个方法已经为只读,但仍能保存进数据库 业务bean已经被@Transactional了。 如果我在save()方法上在注明@Transactional(propagation=Propagation.SUPPORTES)的话,可以生效!! 什么原因?貌似管理也有啊!!!
解决方案三:
bean只被@Transactional标注而已,并没有指定propagation,实际上在执行save方法是并没有开启事务,所以readonly没起作用,仍然可以保存。