Spring Hibernate事务管理

Spring Hibernate事务管理,spring,hibernate,transactions,Spring,Hibernate,Transactions,我刚刚开始使用spring和hibernate制作一个项目。我的DAO层类扩展了HibernateDaoSupport。我们没有使用注释。 早些时候,我们使用的是struts,因此我们使用了getTransaction、commit、rollback。。会话类提供的方法。 我的要求非常简单,对于所有DAO类,如果有异常,请回滚,否则请提交。请推荐一种引入spring事务管理的最简单方法 你的问题中有几点不清楚。我的解释基于以下假设: 您正在使用spring创建数据源和会话工厂 您正在使用Jav

我刚刚开始使用spring和hibernate制作一个项目。我的DAO层类扩展了HibernateDaoSupport。我们没有使用注释。 早些时候,我们使用的是struts,因此我们使用了getTransaction、commit、rollback。。会话类提供的方法。
我的要求非常简单,对于所有DAO类,如果有异常,请回滚,否则请提交。请推荐一种引入spring事务管理的最简单方法

你的问题中有几点不清楚。我的解释基于以下假设:

  • 您正在使用spring创建数据源和会话工厂
  • 您正在使用Java5或更高版本,并且可以使用注释
下面是您的spring配置的外观

    <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"  />
下面的代码显示了如何使用spring对AOP的支持而不是注释来实现事务管理

    <!-- 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>


非常感谢您提供了清晰准确的答案。此示例是使用注释的快速演示。但在这里,我要注释每一种方法。我需要所有类的通用行为。是否可以在xml中定义它,比如说。。。。我所有的刀都将遵循……传播=传播。必需。。。。。。。没有注释的帮助。我想注释是唯一的出路。对我的需求实现了同样的功能。使用SpringAOP确实可以获得所需的行为。如果您决定使用注释,需要注意的是您不必对每个方法都进行注释。您可以只注释您的类,所有方法都继承该行为,您可以注释一个方法来重写该行为。
    <!-- 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>