如何通过spring在service层配置事务?
本人对hibernate和spring不是太熟,不知道如何用spring声明配置事务。因为我的一个Service实现类需要对多个表进行写入操作,一旦某个表操作失败可以回滚。具体部分代码贴上来,希望放谁可以帮我解决这个问题,没有分可以给大家了,谢谢!1.service的实现类方法(想在这个加上事务)
/**
* 新增阀门并保存
*/
@Override
public void save(CompanyEntity company, SamplerEntity sampler, ValveEntity entity){
//希望在这三步中有一步出错,可以回滚
if(company != null && sampler != null && entity != null ){
valveDao.save(entity); //操作第一张表
samplerDao.save(sampler); //操作第二张表
companyDao.save(company); //操作第三张表
}else{
throw new ProgramException("对象为空");
}
}
2.spring的配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www. xmlns:xsi="http://www.
xmlns:p="http://www. xmlns:context="http://www.
xmlns:jee="http://www. xmlns:aop="http://www.
xmlns:tx="http://www.
xsi:schemaLocation="
http://www. http://www.
http://www. http://www.
http://www. http://www.
http://www. http://www.
http://www. http://www.
<context:annotation-config /><!-- 通过@Resource标记来注入 bean -->
<context:component-scan base-package="com.rlht" />
<context:property-placeholder location="classpath:system.properties"/>
<bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource"
p:driver="${hibernate.connection.driver_class}"
p:driverUrl="${hibernate.connection.url}"
p:user="${hibernate.connection.username}"
p:password="${hibernate.connection.password}"
p:alias="${proxool.pool_alias}"
p:houseKeepingTestSql="${proxool.house-keeping-test-sql}"
p:testBeforeUse="${proxool.test-before-use}"
p:maximumConnectionCount="${proxool.maximum-connection-count}"
p:minimumConnectionCount="${proxool.minimum-connection-count}"
p:prototypeCount="${proxool.prototype-count}"
p:trace="true"/>
<bean name="lobHandler"
class="org.springframework.jdbc.support.lob.DefaultLobHandler"
lazy-init="default">
</bean>
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
p:dataSource-ref="dataSource">
<property name="mappingDirectoryLocations">
<list>
<value>classpath:com/rlht</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
<prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop>
<prop key="hibernate.hbm2dll.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
<property name="eventListeners">
<map>
<entry key="merge">
<bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener"/>
</entry>
</map>
</property>
</bean>
<!--bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean" /-->
<bean id="hibernateTxManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory"/>
<!--bean id="jtaTxManager" class="org.springframework.transaction.jta.JtaTransactionManager" p:userTransaction="jotm"/-->
<aop:config>
<aop:pointcut id="serviceOperation" expression="execution(* com.rlht*.service.*.*(..))"/>
<aop:advisor pointcut-ref="serviceOperation" advice-ref="tx-advice-hibernate"/>
</aop:config>
<tx:advice id="tx-advice-hibernate" transaction-manager="hibernateTxManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<context:mbean-export/>
</beans>