Spring事务——使用XML Schema配置事务策略

Spring同时支持编程式事务策略和声明式事务策略,通常推荐采用声明式事务策略。具体实现过程如下:

1.定义一个接口NewsDao,代码如下:

package com.hyq.chapter08_06_2.dao;

public interface NewsDao {

    public void  insert(String title,String content);
    
}

2.定义接口的实现类NewsDaoImpl,代码如下:

 1 package com.hyq.chapter08_06_2.dao.impl;
 2 
 3 import javax.sql.DataSource;
 4 
 5 import org.springframework.jdbc.core.JdbcTemplate;
 6 
 7 import com.hyq.chapter08_06_2.dao.NewsDao;
 8 public class NewsDaoImpl implements NewsDao{
 9 
10     private DataSource ds;
11     
12     public void setDs(DataSource ds) {
13         this.ds = ds;
14     }
15 
16     @Override
17     public void insert(String title, String content) {
18         // TODO Auto-generated method stub
19         JdbcTemplate jt = new JdbcTemplate(ds);
20         jt.update("insert into news_inf values(1,?,?)",title,content);
21         //两次插入的数据违反了唯一性约束
22         jt.update("insert into news_inf values(1,?,?)",title,content);
23         
24         //如果没有事务,则第一条记录可以被插入
25         //如果增加事务控制,将发现第一条记录也插不进去
26     }
27 
28     
29     
30     
31 }

3.定义配置文件bean.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns="http://www.springframework.org/schema/beans"
 4     xmlns:aop="http://www.springframework.org/schema/aop"
 5     xmlns:p="http://www.springframework.org/schema/p"
 6     xmlns:cache="http://www.springframework.org/schema/cache"
 7     xmlns:context="http://www.springframework.org/schema/context"
 8     xmlns:tx="http://www.springframework.org/schema/tx"
 9     xsi:schemaLocation="http://www.springframework.org/schema/beans
10     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
11     http://www.springframework.org/schema/context
12     http://www.springframework.org/schema/context/spring-context-4.0.xsd
13     http://www.springframework.org/schema/aop
14     http://www.springframework.org/schema/aop/spring-aop.xsd
15     http://www.springframework.org/schema/tx
16     http://www.springframework.org/schema/tx/spring-tx.xsd
17     http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
18     
19     
20     <!-- 定义数据源Bean,使用C3P0数据源实现,并注入数据源的必要信息 -->
21     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
22         destroy-method="close"
23         p:driverClass="com.mysql.jdbc.Driver"
24         p:jdbcUrl="jdbc:mysql://localhost/db_spring"
25         p:user="root"
26         p:password="111"
27         p:maxPoolSize="40"
28         p:minPoolSize="2"
29         p:initialPoolSize="2"
30         p:maxIdleTime="30"/>
31     
32     
33     <!-- 配置JDBC 数据源的局部事务管理器,使用DataSourceTransactionManager类 -->
34     <!-- 该类实现PlatformTransactionManager接口,是针对采用数据源连接的特定实现 -->
35     <!-- 配置DataSourceTransactionManager时需要依赖注入DataSource的引用 -->
36     <bean id="transactionManager"
37         class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
38         p:dataSource-ref="dataSource"/>
39         
40        <!-- 配置一个业务逻辑Bean -->
41        <bean id="newsDao" class="com.hyq.chapter08_06_2.dao.impl.NewsDaoImpl" p:ds-ref="dataSource"/>
42        
43        <!-- 配置事务增强处理Bean,指定事务管理器 -->
44        <tx:advice id="txAdvice"
45            transaction-manager="transactionManager">
46            <!-- 用于配置详细的事务定义 -->
47            <tx:attributes>
48                <!-- 所有以get开头的方法都是只读的 -->
49                <tx:method name="get*" read-only="true"/>
50                <!-- 其他方法用默认的事务设置,指定超时时长为5秒 -->
51                <tx:method name="*" isolation="DEFAULT" propagation="REQUIRED" timeout="5"/>
52            </tx:attributes>
53        </tx:advice>
54        
55        <!-- AOP配置的元素 -->
56        <aop:config>
57            <aop:pointcut expression="execution(* com.hyq.chapter08_06_2.dao.impl.*Impl.*(..))" id="myPointcut"/>
58            <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut"/>
59        </aop:config>
60 </beans>

4.编写一个测试类SpringTest

 1 package com.hyq.chapter08_06_2;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 import com.hyq.chapter08_06_2.dao.NewsDao;
 7 
 8 
 9 public class SpringTest {
10 
11     public static void main(String[] args) {
12         ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
13         NewsDao dao = (NewsDao)ctx.getBean("newsDao",NewsDao.class);
14         dao.insert("标题", "测试插入的内容");
15     }
16 }

通过执行该测试程序可以看出NewsDaoImpl这个类的方法已经具备了事务性,即当插入过程中出现异常时,查看数据库可以发现一条记录也没插入成功,如果程序不具备事务性的话,则查看数据库会发现程序能够插入一条数据。

PS:如果想用注解来配置事务,则可以把上面bean.xml中的43到59行的代码替换成如下配置文件:

<!--   其中<tx:annotation-driven/>的 transaction-manager属性的默认值为transactionManager     -->
<tx:annotation-driven/>
然后在dao的实现类,即NewsDaoImpl.java的class前面加上事务的注释@Transactional(),这样的话,该dao实现类的所有方法都具有事务性
原文地址:https://www.cnblogs.com/zest/p/5883974.html