`
墨香子
  • 浏览: 46309 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

使用spring的annotation配置事务

阅读更多
首先配置Hibernate的局部事务管理,使用HibernateTransactionManager类,该类实现了PlatformTransactionManager接口,是针对Hibernate的特定实现,如下添加:
    <bean id="transactionManager"
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

另外需要开启annotation支持,添加如下配置:
<tx:annotation-driven transaction-manager="transactionManager"/>

添加完成后我的applicationContext.xml文件如下:
<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    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.0.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    <!-- derby创建用户名和密码参考:http://www.joyzhong.com/archives/643 -->
    <bean
        id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName">
            <value>org.apache.derby.jdbc.EmbeddedDriver</value>
        </property>
        <property name="url">
            <value>jdbc:derby:f:/zwh/mydb2</value>
        </property>
        <property name="username">
            <value>root</value>
        </property>
        <property name="password">
            <value>root</value>
        </property>
    </bean>
    <bean
        id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource"/>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect"> org.hibernate.dialect.DerbyDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
            </props>
        </property>
        <property name="annotatedClasses">
            <list>
                <value>zwh.ssh.maven.po.User</value>
                <value>zwh.ssh.maven.po.Account</value>
                <value>zwh.ssh.maven.po.Book</value>
            </list>
        </property>
    </bean>
    <!-- 使用annotation的方式配置Spring Bean -->
    <context:component-scan base-package="zwh.ssh.maven"></context:component-scan>
    <!-- 使用注解方式的aop -->
    <bean class="zwh.ssh.maven.aop.ServiceAdvice"></bean>
    <bean class="zwh.ssh.maven.aop.DaoAdvice"></bean>
    <aop:aspectj-autoproxy/>
    <!-- 开启声明式事务,并采用注解的方式 -->
    <bean id="transactionManager"
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

使用事务时,只需要在某方法上使用@Transactional标注即可,例如:
@Service("accountService")
public class AccountServiceImpl implements AccountService {
	@Autowired
	private AccountDao accountDao;
	@Autowired
	private BookDao bookDao;
	
	public AccountDao getAccountDao() {
		return accountDao;
	}

	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}
	
	public BookDao getBookDao() {
		return bookDao;
	}

	public void setBookDao(BookDao bookDao) {
		this.bookDao = bookDao;
	}

	@Override
	public Account getAccountByUserID(int user_id) {
		Account account = this.accountDao.getAccountByUserID(user_id);
		return account;
	}
	
	@Transactional
	@Override
	public void buyBooks(Account account, Book book, int num) {
		account.setBalance(account.getBalance() - book.getPrice()*num);
		book.setAmount(book.getAmount() - num);
		this.bookDao.update(book);
		this.accountDao.update(account);
	}
}

上面的buyBooks模拟了一次购买书的过程,在这次购书过程中需要两次与数据库交互,首先调用bookDao的update方法减少书的数量,然后调用accountDao的update方法减少账户余额。如果该过程不采用事务,假设某一次买书的过程中书的数量是充足的,而账户的余额不足,那么就会出现书的数量减少了而余额却仍是原来的余额,这肯定出问题。加上事务之后就不会出现了上面的问题了,如果这次执行中出现了异常,那么事务的全部操作都被回滚。
分享到:
评论

相关推荐

    Spring JDBC与事务管理

    (2)(2) 在BookShopDao中添加一个purchase购书方法,其操作流程是获取书的单价-&gt;买书(更新库存)-&gt;更新账户余额,在BookShopDaoImp中实现该方法,并使用基于XML和Annotation的声明式事务管理来确保该购书过程能正常...

    spring的annotation-driven配置事务管理器详解 (多数据源配置

    spring的annotation-driven配置事务管理器详解 (多数据源配置

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

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

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

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

    JavaEE的Spring JDBC与事务管理 实验

    (2) 在BookShopDao中添加一个purchase购书方法,其操作流程是获取书的单价-&gt;买书(更新库存)-&gt;更新账户余额,在BookShopDaoImp中实现该方法,并使用基于XML和Annotation的声明式事务管理来确保该购书过程能正常执行...

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

    一个小实例工程,说的是spring3+hibernate4配置声明式事务管理(annotation方式)

    Spring MVC事务配置

    一、      XML,使用tx标签配置拦截器实现事务 一、      Annotation方式 以下所使用环境为Spring4.0.3、Hibernate4.3.5

    Spring 事务配置详解(多种配置方法)

    Spring事务配置的例子。有多种方法,有传统的XML,还有Annotation等等

    Spring JDBC与事务管理源代码

    1、掌握Spring JDBC的配置; 2、掌握JdbcTemplae类中增删改查方法的使用; 3、了解Spring事务管理的3个核心接口; 4、了解Spring事务管理的两种方式;...5、掌握基于XML和Annotation的声明式事务管理的使用。

    基于java的企业级应用开发:声明式事务管理.ppt

    使用@Transactional注解时,可以通过参数配置事务详情: 5.2.2 基于Annotation方式的声明式事务 * * * * * * * * 声明式事务管理 如何实现Spring的声明式事务管理? 5.2 声明式事务管理 Spring的声明式事务管理可以...

    Spring 2.x配置详解

    Spring2.5提供了更灵活的配置方法, 本文档详细介绍了Spring2.x通过XML文件和Annotation如何配置Spring bean,事务管理配置,AOP的详细配置,以及其他Spring所提供支持的配置。

    Spring的学习笔记

    一、 开始使用annotation配置Spring 16 二、 @Autowired、@Qualifier 16 (一) @Autowired 16 (二) @Qualifier 17 三、 @Resource(重要、推荐) 17 (一) JSR-250 17 (二) @Resource 17 四、 @Componet 18 五、 @Scope...

    Spring4.1-HibernateAnnotation:Hibernate与Spring整合 使用注解配置事务

    Spring4.1-HibernateAnnotation &gt;记住在配置事务管理器时,要设置sessionFactory

    Spring中文帮助文档

    6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点运算 ...

    Spring API

    6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点运算 ...

    spring2.5 学习笔记

    一、 开始使用annotation配置Spring 16 二、 @Autowired、@Qualifier 16 (一) @Autowired 16 (二) @Qualifier 17 三、 @Resource(重要、推荐) 17 (一) JSR-250 17 (二) @Resource 17 四、 @Componet 18 五、 @Scope...

    Spring.3.x企业应用开发实战(完整版).part2

    4.11.2 使用基于Java类的配置信息启动Spring容器 4.12 不同配置方式比较 4.13 小结 第5章 Spring容器高级主题 5.1 Spring容器技术内幕 5.1.1 内部工作机制 5.1.2 BeanDefinition 5.1.3 InstantiationStrategy 5.1.4 ...

    spring applicationContext 配置文件

    &lt;bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:order="0"&gt; &lt;property name="interceptors"&gt; &lt;list&gt; &lt;ref bean="doubleSubmitInterceptor"/&gt; &lt;/list&gt; ...

    spring2.5.chm帮助文档(中文版)

    2.2.4. Annotation(注解)驱动配置 2.2.5. 在classpath中自动搜索组件 2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的支持 2.3.3. 对bean命名pointcut( bean name pointcut ...

    Spring3中配置DBCP,C3P0,Proxool,Bonecp数据源

    在Spring3中配置数据源,包括DBCP,C3P0,Proxool,Bonecp主要的数据源,里面包含这些数据源的jar文件和依赖文件及配置文件。。 如Bonecp目前听说是最快的数据源,速度是传统的c3p0的25倍, bonecp.properties文件: ...

Global site tag (gtag.js) - Google Analytics