spring提供的事务管理可以分为两类:编程式的和声明式的。编程式的,比较灵活,但是代码量大,存在重复的代码比较多;声明式的比编程式的更灵活。很多时候都会用到他的声明式事务,简单的在配置文件中进行一些规则配置,利用Spring的AOP功能就能轻松搞定事务问题;这里面涉及到一个事务的传播属性问题【Propagation】,他在TransactionDefinition接口中定义,共有7种选项可用:
PROPAGATION_REQUIRED:支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
PROPAGATION_SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行。
PROPAGATION_MANDATORY:支持当前事务,如果当前没有事务,就抛出异常。
PROPAGATION_REQUIRES_NEW:新建事务,如果当前存在事务,把当前事务挂起。
PROPAGATION_NOT_SUPPORTED:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
PROPAGATION_NEVER:以非事务方式执行,如果当前存在事务,则抛出异常。
PROPAGATION_NESTED:支持当前事务,新增Savepoint点,与当前事务同步提交或回滚。
以下为一例:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="oracle.jdbc.driver.OracleDriver">
</property>
<property name="url"
value="jdbc:oracle:thin:@192.168.1.1:1521:ASDF">
</property>
<property name="username" value="ASDFF"></property>
<property name="password" value="ASDFF"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>.../vo/Attachment.hbm.xml</value>
<value>.../vo/Author.hbm.xml</value>
<value>.../vo/Docs.hbm.xml</value>
<value>.../vo/FileForder.hbm.xml</value>
<value>.../vo/Files.hbm.xml</value>
<value>.../vo/Users.hbm.xml</value>
</list>
</property>
</bean>
<!-- DAO接口的实现类 -->
<bean id="fileDaoTarget" class=".......dao.FileDAO">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<!-- Serv接口的实现类 -->
<bean id="fileService" class="......serv.FileService">
<property name="fileDao">
<ref bean="fileDao"/>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="fileDao" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="proxyInterfaces">
<list>
<!-- 代理接口 -->
<value>.......dao.IFileDAO</value>
</list>
</property>
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="target">
<ref bean="fileDaoTarget"/>
</property>
<property name="transactionAttributeSource">
<ref bean="transactionAttributeSource"/>
</property>
</bean>
<!-- 事务属性配置 -->
<bean id="transactionAttributeSource" class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
<property name="properties">
<props>
<prop key="edit*">PROPAGATION_REQUIRED</prop>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>
使用AOP:
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="oracle.jdbc.driver.OracleDriver">
</property>
<property name="url"
value="jdbc:oracle:thin:@192.168.1.1:1521:asdf">
</property>
<property name="username" value="asdf"></property>
<property name="password" value="asdf"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>.../vo/Attachment.hbm.xml</value>
<value>.../vo/Author.hbm.xml</value>
<value>.../vo/Docs.hbm.xml</value>
<value>.../vo/FileForder.hbm.xml</value>
<value>.../vo/Files.hbm.xml</value>
<value>.../vo/Users.hbm.xml</value>
</list>
</property>
</bean>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="daoOperation" expression="execution(* dao.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="daoOperation"/>
</aop:config>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="fileDao" class="..dao.FileDAO">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="fileService" class="..FileService">
<property name="fileDao">
<ref bean="fileDao"/>
</property>
</bean>
</beans>
事务异常的捕捉及处理可以在切入点的包围进行,如:
@Transaction
public boolean saveFile(Files file, Files file2)
{
this.save(file);
this.save(file2);
return true;
}
捕捉异常:
public boolean saveFile(Files file, Files file1)
{
try{
return this.getFileDao().saveFile(file, file1);
}catch(Exception e)
{
log.error(e);
return false;
}
}
分享到:
相关推荐
spring3,hibernate4 配置声明式事务管理(annotation方式)
本资源主要是结合博文 Spring+Hibernate: 声明式事务 http://blog.csdn.net/ManagementAndJava/article/details/78050519 的源代码,主要是讲解了spring4.3和hibernate5.2.11集成实现原理;
Spring_Hibernate使用TransactionInterceptor声明式事务配置.doc
Spring与Hibernate集成声明式事务.ppt
NULL 博文链接:https://quicker.iteye.com/blog/674029
一个实例小工程,说的是spring3+hibernate4怎么配置声明式事务管理(xml方式)
声明式事务控制,spring2.5+hibernate3集成源码,附带有数据库脚本,可以直接导入到项目的工作空间。欢迎下载
在Spring3中配置声明式事务比早期版本显得更加简便。只需要几行配置文件+注解就可以实现面向切面的AOP事务
一个小实例工程,说的是spring3+hibernate4配置声明式事务管理(annotation方式)
Spring+Hibernate 使用TransactionInterceptor声明式事务配置
spring3、 hibernate4 配置声明式事务管理(annotation方式)
spring2.5 + hibernate3.3 声明式事务 和所需jar包,spring2.5 + hibernate3.3 声明式事务 和所需jar包,spring2.5 + hibernate3.3 声明式事务 和所需jar包,
NULL 博文链接:https://chenming47.iteye.com/blog/437199
Spring+Hibernate注解声明式事务实例
ssh三大框架的整合步骤,非常详细。包含spring整合struts、spring整合hibernate、声明式事务说明等。
1.掌握Myeclipse的使用。 2.掌握spring框架和hibernate框架的使用。 3. 掌握整合spring和hibernate的持久化操作编程 4.掌握基于AOP的声明式事务编程...3.配置WEB-INF/applicationContext.xml提供基于AOP的声明式事务
* 声明式事务管理: * 基于TransactionProxyFactoryBean的方式.(很少使用) * 需要为每个进行事务管理的类,配置一个TransactionProxyFactoryBean进行增强. * 基于AspectJ的XML方式.(经常使用) * 一旦配置好之后,...
Spring4整合Hibernate4实现用户购买图书和结账等操作,整合主要实现用IoC容器来管理Hibernate的SessionFactory实例,并使Hibernate使用Spring所提供的声明式事务……
Spring4.0、Struts2.3.15、Hibernate4.2.4、jQuery1.9.1涉及到了诸多开发时的细节:ModelDriven、Preparable 拦截器、编写自定义的类型转换器、Struts2 处理 Ajax、OpenSessionInViewFilter、迫切左外连接、Spring ...