Spring Hibernate事务管理 [英] Spring Hibernate transaction management

查看:111
本文介绍了Spring Hibernate事务管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始用spring和hibernate做一个项目。我的DAO图层类扩展了HibernateDaoSupport。我们不使用注释。
之前,我们使用的是struts,因此我们使用了Session类提供的getTransaction,commit,rollback等方法。
我的要求非常简单,对于所有DAO类,如果有异常,则回滚否则提交。请建议介绍spring事务管理的最简单方法。

解决方案

有几件事情在您的问题中不清楚。我的解释基于以下假设 -


  • 您正在使用spring创建数据源和会话工厂

  • 您正在使用Java 5或更高版本并可以使用注释。



以下是您的弹簧配置的外观。

 < bean id =myDataSourceclass =org.apache.commons.dbcp.BasicDataSource
destroy -method = 关闭 >
< property name =driverClassNamevalue =org.hsqldb.jdbcDriver/>
< property name =urlvalue =jdbc:hsqldb:hsql:// localhost:9001/>
< property name =usernamevalue =sa/>
< property name =passwordvalue =/>
< / bean>

< bean id =mySessionFactory
class =org.springframework.orm.hibernate3.LocalSessionFactoryBean>
< property name =dataSourceref =myDataSource/>
< property name =mappingResources>
< list>
< value> product.hbm.xml< /值>
< / list>
< / property>
< property name =hibernateProperties>
<值>
hibernate.dialect = org.hibernate.dialect.HSQLDialect
< / value>
< / property>
< / bean>

< bean id =transactionManagerclass =org.springframework.orm.hibernate3.HibernateTransactionManager>
< property name =sessionFactoryref =mySessionFactory/>
< / bean>

< tx:注解驱动的事务管理器=transactionManager/>

设置完成后,您可以在您的DAO方法上使用spring transactional annotations,如下所示。 Spring将处理启动事务,提交事务或在抛出异常时回滚事务。如果您有商业服务,您最好在您的服务上使用交易注释,而不是DAO。

  @Transactional(propagation = Propagation。需要)
public class MyTestDao扩展了HibernateDaoSupport {
public void saveEntity(Entity entity){
getHibernateTemplate()。save(entity);

@Transactional(readOnly = true)
public实体getEntity(Integer id){
return getHibernateTemplate()。get(Entity.class,id);






$ p

下面的代码显示了如何使用spring的事务管理实现事务管理支持AOP而不是注释。

 <! - 定义您的'myDatasource'bean和'mySessionFactory'bean,如前面的代码片段 - > 
<! - 然后按照下面的步骤 - >

< bean id =transactionManager
class =org.springframework.orm.hibernate3.HibernateTransactionManager>
< property name =sessionFactoryref =mySessionFactory/>

<! - 这是我们想要进行交易的道对象 - >
< bean id =testDaoclass =com.xyz.daos.MyTestDao/>

<! - 交易建议 - >
< tx:advice id =txAdvicetransaction-manager =transactionManager>
< tx:attributes>
<! - 所有以'get'开头的方法都是只读 - >
< tx:方法名称=get *只读=true/>
<! - 其他方法使用默认的交易设置(见下文) - >
< tx:method name =*propagation =REQUIRED/>
< / tx:属性>
< / tx:advice>

<! - 确保上述交易通知运行于任何执行
'daos'包中的方法 - >
< aop:config>
expression =execution(* com.xyz.daos。*(..))/>
< / aop:config>

有关更多详细信息,请参阅 -
Spring Declarative Transactions


I have just started making a project using spring and hibernate. My DAO layer class extends HibernateDaoSupport. We are not using annotations. Earlier, we were using struts, hence we used getTransaction, commit, rollback .. methods provided by Session class. My requirement is very simple, for all DAO classes, if there is an exception, rollback otherwise commit. Please suggest a simplest way of introducing spring transaction management.

解决方案

A few things are not clear from your question. My explanation follows based on below assumptions -

  • You are using spring to create a datasource and session factory
  • You are using Java 5 or above and could use annotations.

Here is what your spring configuration would look like.

    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
    <property name="url" value="jdbc:hsqldb:hsql://localhost:9001" />
    <property name="username" value="sa" />
    <property name="password" value="" />
</bean>

<bean id="mySessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource" />
    <property name="mappingResources">
        <list>
            <value>product.hbm.xml</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <value>
            hibernate.dialect=org.hibernate.dialect.HSQLDialect
        </value>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="mySessionFactory" />
</bean> 

<tx:annotation-driven transaction-manager="transactionManager"  />

Once this is set up, you could use spring transactional annotations on your DAO methods as shown below. Spring would take care of starting transactions, committing your transactions or rolling back your transactions when exceptions are thrown. If you have business services, you would ideally use transactional annotations on your services instead of DAOs.

@Transactional(propagation=Propagation.REQUIRED)
public class MyTestDao extends HibernateDaoSupport {    
public void saveEntity(Entity entity){
    getHibernateTemplate().save(entity);
}
@Transactional(readOnly=true)
public Entity getEntity(Integer id){
    return getHibernateTemplate().get(Entity.class, id);
}
 }

Below code shows how transaction management could be achieve using spring's support for AOP rather than annotations.

    <!-- Define your 'myDatasource' bean and 'mySessionFactory' bean as shown in previous code snippet -->
<!--  Then follow the steps shown below -->

<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory" />   

<!-- this is the dao object that we want to make transactional -->
<bean id="testDao" class="com.xyz.daos.MyTestDao" />

<!-- the transactional advice  -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <!-- all methods starting with 'get' are read-only -->
        <tx:method name="get*" read-only="true" />
        <!-- other methods use the default transaction settings (see below) -->
        <tx:method name="*" propagation="REQUIRED" />
    </tx:attributes>
</tx:advice>

<!-- ensure that the above transactional advice runs for any execution of 
    a method in 'daos' package-->
<aop:config>
    <aop:pointcut id="allDaoMethods"
        expression="execution(* com.xyz.daos.*(..))" />
    <aop:advisor advice-ref="txAdvice" pointcut-ref="allDaoMethods" />
</aop:config>

For additional details, please see - Spring Declarative Transactions

这篇关于Spring Hibernate事务管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆