Spring事务(transaction)

Spring事务技术点: ThreadLocal,保证同一个线程是同一个连接

AOP,代理方式(拦截所有方法)+事务

实现:通过xml or 注解方式:

1) XML方式

------>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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx       //添加事务管理器配置
http://www.springframework.org/schema/tx/spring-tx.xsd">     //添加事务管理器配置
<context:component-scan base-package="jdbc"></context:component-scan>    //自动生成对象
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring" />
<property name="user" value="root" />
<property name="password" value="Ok25897758" />
<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="1" />
<!--连接池中保留的最小连接数。 -->
<property name="minPoolSize" value="1" />
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="300" />
<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="60" />
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="5" />
<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
<property name="idleConnectionTestPeriod" value="60" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="txManager"       //创建事务管理器对象
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="txManager"/>    //添加注解

</beans>

--------->数据层

@Repository("customerDao")      //数据层用repository
public class CustomerDaoBean implements CustomerDao {
@Resource(name="jdbcTemplate")
private JdbcTemplate jdbcTemplate;

--------->业务层

@Service("customerService")
public class CustomerServiceImpl implements CustomerService {
@Resource(name="customerDao")
private CustomerDao customerDao;

set方法()省略

@Override
@Transactional    //哪个方法用事务哪个方法添加此注解

                         //@Transactional(propagation=Propagation.NOT_SUPPORTED)表示此方法不使用事务
public void addCustomer(Customer customer) {

测试类:

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("jdbc/bean5.xml");
CustomerService customerService = applicationContext.getBean("customerService", CustomerService.class);

特别注意:此处用接口类, 而"customerService"是接口的实现类的对象!

2)注解方式

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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring" />
<property name="user" value="root" />
<property name="password" value="Ok25897758" />
<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="1" />
<!--连接池中保留的最小连接数。 -->
<property name="minPoolSize" value="1" />
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="300" />
<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="60" />
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="5" />
<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
<property name="idleConnectionTestPeriod" value="60" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="customerDao" class="jdbc.CustomerDaoBean">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="customerService" class="jdbc.CustomerServiceImpl">
<property name="customerDao" ref="customerDao"></property>
</bean>
<aop:config>                    //添加切点和通知(advisor=advice+pointcut)
    <aop:pointcut expression="execution(* jdbc.*.*(..))" id="transactionPointcut"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut"/>
</aop:config>
<tx:advice id="txAdvice" transaction-manager="txManager">      //添加拦截方式---->事务管理器
   <tx:attributes>
      <tx:method name="find*" read-only="true" propagation="NOT_SUPPORTED"/>    //find*开头的方法会拦截但不产生事务
      <tx:method name="*"/>                                                                                 //其他所有方法有事务
   </tx:attributes>
</tx:advice>

</beans>

posted @ 2016-06-21 09:38  乱世_独自美  阅读(112)  评论(0)    收藏  举报