Spring3开发实战 之 第五章:Spring中的事务
Spring框架引人注目的重要因素之一是它全面的事务支持。Spring框架提供了一致的事务管理抽象,这带来了以下好处:1:为复杂的事务API提供了一致的编程模型,如JTA、JDBC、Hibernate、JPA和JDO2:支持声明式事务管理3:提供比复杂的事务API(诸如JTA)更简单的、更易于使用的编程式事务管理API4:非常好地整合Spring的各种数据访问抽象Spring事务抽象的关键是事务策略的概念。这个概念由org.springframework.transaction.PlatformTransactionManager接口定义:
java代码:查看复制到剪贴板打印
- public interface PlatformTransactionManager {
- TransactionStatus getTransaction(TransactionDefinition definition)
- throws TransactionException;
- void commit(TransactionStatus status) throws TransactionException;
- void rollback(TransactionStatus status) throws TransactionException;
- }
- ge
tTransaction(..)方法根据一个类型为 TransactionDefinition 的参数返回一个 TransactionStatus 对象。返回的 TransactionStatus 对象可能代表一个新的或已经存在的事务(如果在当前调用堆栈有一个符合条件的事务。如同J2EE事务环境,一个 TransactionStatus 也是和执行 线程 绑定的)TransactionDefinition接口指定1:事务隔离:当前事务和其它事务的隔离的程度。例如,这个事务能否看到其他事务未提交的写数据?2:事务传播:通常在一个事务中执行的所有代码都会在这个事务中运行。但是,如果一个事务上下文已经存在,有几个选项可以指定一个事务性方法的执行行为:例如,简单地在现有的事务中继续运行(大多数情况);或者挂起现有事务,创建一个新的事务。Spring提供EJB CMT中常见的事务传播选项。3:事务超时: 事务在超时前能运行多久(自动被底层的事务基础设施回滚)。4:只读状态: 只读事务不修改任何数据。只读事务在某些情况下(例如当使用Hibernate时),是一种非常有用的优化。


java代码:查看复制到剪贴板打印
- <bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName">
- <value>oracle.jdbc.driver.OracleDriver</value></property>
- <property name="url">
- <value>jdbc:oracle:thin:@localhost:1521:orcl</value></property>
- <property name="username"> <value>test</value> </property>
- <property name="password" value="test"/>
- </bean>
- <bean id="txManager"
- class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/>
- </bean>
java代码:查看复制到剪贴板打印
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:jee="http://www.springframework.org/schema/jee"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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
- http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
- ">
- <jee:jndi-lookup id="dataSource" jndi-name="jdbc/jpetstore"/>
- <bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager" />
- </beans>
java代码:查看复制到剪贴板打印
- <bean id="txManager“
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
java代码:查看复制到剪贴板打印
- <bean id="txManager"
- class="org.springframework.transaction.jta.JtaTransactionManager"/>
java代码:查看复制到剪贴板打印
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- 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/context http://www.springframework.org/schema/context/spring-context-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="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource"/>
- </bean>
- <tx:advice id="txAdvice" transaction-manager="txManager">
- <tx:attributes>
- <tx:method name="get*" read-only="true"/>
- <tx:method name="*"/>
- </tx:attributes>
- </tx:advice>
- <aop:config>
- <aop:pointcut id="myPointCut" expression="execution(* cn.javass.spring3.jdbc.Impl.*(..))"/>
- <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointCut"/>
- </aop:config>
- <bean name="api" class="cn.javass.spring3.jdbc.Impl">
- <property name="ds" ref="dataSource"></property>
- </bean>
- <bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName">
- <value>oracle.jdbc.driver.OracleDriver</value>
- </property>
- <property name="url">
- <value>jdbc:oracle:thin:@localhost:1521:orcl</value>
- </property>
- <property name="username"> <value>ztb</value> </property>
- <property name="password" value="ztb"/>
- </bean>
- </beans>
java代码:查看复制到剪贴板打印
- <tx:advice id="txAdvice" transaction-manager="txManager">
- <tx:attributes>
- <tx:method name="get*" read-only="false"
- rollback-for="NoProductInStockException"/>
- <tx:method name="*"/>
- </tx:attributes>
- </tx:advice>

- 2楼wm59205天前 17:33
- 飘过。。。。
- 1楼han_yankun20095天前 17:23
- 学习了