Spring 中的事务管理

来源:互联网 发布:石家庄淘宝代运营 编辑:程序博客网 时间:2024/05/17 22:56

Spring框架对事务管理进行了高层次的抽象,定义了各种类型的事务管理器,用来实现事务管理功能。

API中定义了PlatformTransactionManager接口,称为平台事务管理器接口,是Spring框架事务管理架构的核心接口,称为平台事务管理器接口,

是Spring框架事务管理架构的核心接口,定义了事务管理器的几本行为,所有的事务管理器都直接或间接实现了该接口。

接口中定义了三个方法:

1、commit方法:该方法用来提交一个事务。

2、getTransaction方法:该方法可以根据事务策略,返回一个TransactionStatus实例

3、rollback方法:该方法用来回滚事务。


编程式事务管理

Spring框架支持编程式事务管理,也就是可以通过编写代码实现事务管理。

可以通过两种方式实现:

使用TransactionTemplate类

实现PlatformTransactionManager接口


改造CustomerServiceImpl类,使register方法的插入记录操作,在一个事务中进行。

1、在CustomerServiceImpl类中声明PlatformTransactionManager类型属性,并提供setter方法。

public class CustomerServiceImpl implements CustomerService{private PlatformTransactionManager txManager;public void setTxManager(PlatformTransactionManager txManager) {this.txManager = txManager;}

2、在applicationContext.xml中配置事务管理器实例

<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory"><ref bean="sessionFactory"/></property></bean><bean id="service" class="service.CustomerServiceImpl"><property name="txManager"><ref bean="txManager"/></property><property name="dao"><ref bean="dao"/></property></bean>

3、在CustomerServiceImpl类的register方法中创建TransactionTemplate对象

4、实现TransactionCallback接口

public void register(final Customer cust)throws RegisterException{System.out.println("invoke register...");TransactionTemplate tTemplate=new TransactionTemplate(txManager);Customer c = dao.selectByName(cust.getCustname());if(c==null){tTemplate.execute(new TransactionCallback(){public Object doInTransaction(TransactionStatus arg0){dao.insert(cust);return null;}});}else{throw new RegisterException();}}
5、使用如下代码进行测试:

public static void main(String[] args) {ApplicationContext ctxt=new ClassPathXmlApplicationContext("applicationContext.xml");CustomerService service = (CustomerService)ctxt.getBean("service");try {service.register(new Customer("la","xia",33,"SY"));//service.login("bian", "xia");} catch (RegisterException e) {// TODO Auto-generated catch blocke.printStackTrace();}List<Customer> list=service.viewAll();for(Customer cust:list){System.out.println(cust.getCustname()+" "+cust.getAddress());}}}

上述代码中调用了service.register方法后,就已经使用了编程式事务管理功能。


声明式事务管理

AOP是Spring框架中除了IoC以外另外一个核心概念,Spring中AOP有两个主要功能:自定义切面;声明式事务管理。

声明式事务管理就是不在源文件中编写Java代码管理事务,而是使用AOP框架,在IoC容器中装配进行事务管理的实例。


1、配置事务管理器

在applicationContext中装配HibernateTransactionManager实例:

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

2、装配TransactionProxyFactoryBean

TransactionProxyFactoryBean类中常用的setter方法:

①setTransactionManager(PlatformTransactionManager transactiontxManager):该方法能够用来指定事务管理使用的事务管理器。

②setTarget(Object target):该方法可以用来指定代理的目标对象,如本例中的CustomerSeerviceImpl实例就是目标对象。

③setTransactionAttributes(Properties transactionAttributes):该方法可以指定事务的属性,属性使用Properties类型进行配置,每一个属性都有key/value键值对表示。

<bean id="serviceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"><property name="transactionManager"><ref bean ="txManager"/></property><property name="target"><ref bean ="service"/></property><property name="transactionAttributes"><props><prop key="register">PROPAGATION_REQUIRED</prop></props></property></bean>
CustomerServiceImpl的register方法:

public void register(final Customer cust)throws RegisterException{System.out.println("invoke register...");//TransactionTemplate tTemplate=new TransactionTemplate(txManager);Customer c = dao.selectByName(cust.getCustname());if(c==null){dao.insert(cust);}else{throw new RegisterException();}


3、测试:

public static void main(String[] args) {ApplicationContext ctxt=new ClassPathXmlApplicationContext("applicationContext.xml");CustomerService service = (CustomerService)ctxt.getBean("serviceProxy");try {service.register(new Customer("gang","zi",23,"TJ"));} catch (RegisterException e) {// TODO Auto-generated catch blocke.printStackTrace();}}



0 0
原创粉丝点击