`
心痛泪流
  • 浏览: 12413 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Spring对Hibernate事务的管理配置

阅读更多

转自:http://www.cnblogs.com/Angi/articles/2007552.html,个人学习备忘!!

1、TransactionProxyFactoryBean

<?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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:hibernate.cfg.xml">
        </property>
    </bean>
    <bean id="TblUserDAO" class="com.angi.dao.TblUserDAO">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>
    <!-- 声明一个 Hibernate 3 的 事务管理器供代理类自动管理事务用 -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref local="sessionFactory" />
        </property>
    </bean>
    <bean id="TblUserDAOProxy"
        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <!-- 注意这个属性, 详细意义请参考Spring文档中的CGLIB部分或者本章的 10.7.2参考资料部分,
               必须为 true使用CGLIB才不用强制编写DAO接口 -->
        <property name="proxyTargetClass">
            <value>true</value>
        </property>
        <property name="transactionManager">
            <ref bean="transactionManager" />
        </property>
        <property name="target">
            <ref local="TblUserDAO" />
        </property>
        <property name="transactionAttributes">
            <props>
                <!-- 这里的方法签名可以精确到方法, 先懒惰一下全配置上 -->
                <prop key="*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean>
</beans>
 

2、TransactionInterceptor

<?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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:hibernate.cfg.xml">
        </property>
    </bean>
    <bean id="tblUserDAO" class="com.angi.dao.TblUserDAO">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>
    <bean id="tblUserService" class="com.angi.dao.service.TblUserService">
        <property name="tblUserDAO">
            <ref bean="tblUserDAO" />
        </property>
    </bean>
    <!-- 声明一个 Hibernate 3 的 事务管理器供代理类自动管理事务用 -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref local="sessionFactory" />
        </property>
    </bean>
    <bean
        class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <property name="beanNames">
            <list>
                <value>tblUserService</value>
            </list>
        </property>
        <property name="interceptorNames">
            <list>
                <value>transactionInterceptor</value>
            </list>
        </property>
    </bean>
    <bean id="transactionInterceptor"
        class="org.springframework.transaction.interceptor.TransactionInterceptor">
        <property name="transactionManager" ref="transactionManager" />
        <property name="transactionAttributes">
            <props>
                <!-- 这里的方法签名可以精确到方法, 先懒惰一下全配置上 -->
                <prop key="*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean>
</beans>

3、AOP和TX配置

<?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:p="http://www.springframework.org/schema/p"
    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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:hibernate.cfg.xml">
        </property>
    </bean>
    <bean id="tblUserDAO" class="com.angi.dao.TblUserDAO">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>
    <bean id="tblUserService" class="com.angi.dao.service.TblUserService">
        <property name="tblUserDAO">
            <ref bean="tblUserDAO" />
        </property>
    </bean>
    <!-- 声明一个 Hibernate 3 的 事务管理器供代理类自动管理事务用 -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref local="sessionFactory" />
        </property>
    </bean>
    <!-- 需要引入aop的命名空间 -->
    <aop:config>
        <!-- 切入点指明了在执行Service的所有方法时产生事务拦截操作 -->
        <aop:pointcut id="daoMethods"
            expression="execution(* com.angi.dao.service.TblUserService.*(..))" />
        <!-- 定义了将采用何种拦截操作,这里引用到 txAdvice -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="daoMethods" />
    </aop:config>
    <!-- 需要引入tx的命名空间 -->
    <!-- 这是事务通知操作,使用的事务管理器引用自 transactionManager -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 指定哪些方法需要加入事务,这里懒惰一下全部加入,可以使用通配符来只加入需要的方法 -->
            <tx:method name="*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>
</beans>

4、anotation

<?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:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
    <!-- 需要引入tx的命名空间 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:hibernate.cfg.xml">
        </property>
    </bean>
    <bean id="tblUserDAO" class="com.angi.dao.TblUserDAO">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>
    <bean id="tblUserService" class="com.angi.dao.service.TblUserService">
        <property name="tblUserDAO">
            <ref bean="tblUserDAO" />
        </property>
    </bean>
    <!-- 声明一个 Hibernate3 的事务管理器供代理类自动管理事务用 -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref local="sessionFactory" />
        </property>
    </bean>
</beans>
 
@Transactional
    public void doTransaction() {
        // step1 insert
        TblUser tblUser1 = new TblUser();
        tblUser1.setId(24);
        tblUser1.setUsername("Angi12");
        tblUser1.setPassword("Wang21");
        tblUserDAO.save(tblUser1);
        // step2 update
        TblUser tblUser2 = tblUserDAO.findById(2);
        tblUser2.setPassword(tblUser2.getPassword() + "a");
        tblUserDAO.update(tblUser2);
        // step3 insert
        TblUser tblUser = new TblUser();
        tblUser.setId(23);
        tblUser.setUsername("Angi");
        tblUser.setPassword("Wang");
        tblUserDAO.save(tblUser);
    }
 

 

分享到:
评论

相关推荐

    spring hibernate 事务管理学习笔记(一)

    在Spring中,我们可以配置`PlatformTransactionManager`接口的实现类,如`HibernateTransactionManager`,它会自动感知Hibernate Session并管理事务。通过声明式事务管理,我们只需在方法上添加`@Transactional`注解...

    Spring Hibernate事务实例

    2. Hibernate事务管理:使用`HibernateTransactionManager`结合SessionFactory进行事务控制。 3. AOP(面向切面编程)在事务管理中的应用:`TransactionInterceptor`基于AOP拦截方法调用,处理事务。 4. 配置事务...

    spring mvc+hibernate实现事务管理(配置文件版)

    本项目是关于如何使用Spring MVC与Hibernate结合来实现事务管理的实践教程,通过MyEclipse自动生成所需的包和配置文件。这里将详细讲解这一过程,以及涉及到的关键知识点。 首先,Spring MVC作为Spring框架的一部分...

    spring整合hibernate实现事务处理

    在`Spring_1800_Spring_Hibernate_Transaction_Annotation`这个压缩包文件中,很可能包含了使用注解方式实现Spring整合Hibernate事务处理的相关示例代码和配置文件。通过阅读和理解这些代码,你可以更好地掌握这一...

    在Spring中配置Hibernate事务

    在Spring框架中集成和配置Hibernate事务管理是企业级Java应用中的常见实践,它能提供高效且灵活的事务处理策略。Spring作为一款强大的依赖注入(DI)和面向切面编程(AOP)容器,能够轻松地管理和协调不同数据访问...

    spring hibernate 事务管理学习笔记(二)

    在本篇“Spring Hibernate 事务管理学习笔记(二)”中,我们将深入探讨Spring框架与Hibernate集成时如何实现高效、安全的事务管理。这是一篇关于源码分析和技术工具使用的文章,适合对Java开发和数据库操作有基础...

    spring3,hibernate4 配置声明式事务管理(annotation方式)

    当我们需要在应用程序中进行事务管理时,Spring提供了一种声明式的方式,使得事务配置更为简洁。本篇将详细介绍如何在Spring 3和Hibernate 4中通过注解来实现声明式事务管理。 首先,我们需要在项目中引入Spring和...

    Spring+Hibernate注解事务实例

    这个实例将涵盖如何创建一个简单的Spring+Hibernate项目,设置事务管理,编写带有@Transactional注解的服务类,以及对应的DAO类。同时,你还会学到如何在单元测试中验证事务处理的正确性。 通过实践这个实例,你...

    Spring与Hibernate集成

    通过Spring对Hibernate的管理,我们可以更好地控制事务、实现解耦,同时降低出错的可能性。此外,Spring的AOP支持使得事务管理更加简单,提高了代码的可测试性。在实际项目中,这种集成方式被广泛应用,是Java EE...

    Spring Hibernate 事务处理 详细说明

    3. **Hibernate事务配置:**在Spring中,需要配置Hibernate SessionFactory,并将其注入到需要进行数据库操作的服务中。同时,通过`PlatformTransactionManager`接口(如HibernateTransactionManager)配置事务管理...

    Spring+Hibernate事务管理

    Spring将事务管理分成了两类: * 编程式事务管理 * 手动编写代码进行事务管理.(很少使用) * 声明式事务管理: * 基于TransactionProxyFactoryBean的方式.(很少使用) * 需要为每个进行事务管理的类,配置一个...

    声明式事务控制spring+hibernate集成

    在"声明式事务控制,spring2.5+hibernate3集成源码"中,开发者可以学习如何配置Spring的事务管理器,以及如何在Hibernate的SessionFactory和SessionFactoryBuilder上使用Spring的TransactionProxyFactoryBean来创建...

    memcache也spring,hibernate的配置

    4. **Spring与Hibernate的集成**:Spring提供了对Hibernate的全面支持,可以通过SessionFactoryBean配置来管理Hibernate的SessionFactory,并通过HibernateTemplate或HibernateJpaDialect进行数据访问操作。...

    spring hibernate mysql 事务实例

    在Spring与Hibernate的结合下,MySQL的事务控制可以通过设置不同的隔离级别(如READ UNCOMMITTED、READ COMMITTED、REPEATABLE READ、SERIALIZABLE)来优化并发性能和避免数据竞争问题。 在实际开发中,我们还需要...

    spring3+hibernate4配置声明式事务管理(annotation方式)

    5. **配置Spring和Hibernate**:在Spring 3中,我们需要配置Hibernate SessionFactory,并将其与Spring的事务管理器相结合。这通常通过XML配置文件或Java配置类完成。同时,需要为数据源、SessionFactory和事务管理...

    springmvc+spring+hibernate

    3. **配置Spring**:在src/main/resources下创建applicationContext.xml文件,配置Spring的核心容器,包括Bean定义、数据源、事务管理器等。例如,使用DataSource来连接Oracle数据库,使用...

    spring配置hibernate事务

    在Spring框架中配置Hibernate事务管理是一项关键任务,它允许开发者以声明式的方式处理数据库操作的事务性,确保数据的一致性和完整性。以下是如何在Spring中配置Hibernate事务的详细步骤和概念解释。 首先,理解...

    Hibernate编程式事务与Spring Aop的声明式事务(spring与hibernate集成)

    Spring通过`LocalSessionFactoryBean`来创建SessionFactory,它会读取Hibernate的配置文件(如hibernate.cfg.xml),并根据其中的设置创建SessionFactory实例。同时,配置数据源是必不可少的,因为它是连接到数据库...

    spring,hibernate整合实现事务管理(MethodInterceptor)

    2. **Hibernate事务管理**: - Hibernate提供了`Session`和`SessionFactory`,它们是与数据库交互的主要对象。`Session`负责单个数据库会话,而`SessionFactory`在整个应用程序生命周期中只创建一次,用于生成`...

    spring3、 hibernate4 配置声明式事务管理(annotation方式)

    `HibernateTransactionManager`是Spring提供的专门用于管理Hibernate事务的类,它会自动处理Hibernate Session和JDBC Connection的开启、提交、回滚。 三、配置Spring事务管理 在Spring的配置文件中,我们需要添加...

Global site tag (gtag.js) - Google Analytics