`

Spring结合Hibernate声明式事务配置

阅读更多

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;
}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics