spring声明式事务管理的配置
来源:互联网 发布:阿拉比卡咖啡豆 知乎 编辑:程序博客网 时间:2024/05/16 01:21
<?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:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!-- this is the service object that we want to make transactional --><bean id="fooService" class="x.y.service.DefaultFooService"/><!-- the transactional advice (what happens; see the <aop:advisor/> bean below) --><tx:advice id="txAdvice" transaction-manager="txManager"><!-- the transactional semantics... --><tx:attributes><!-- all methods starting with get are read-only --><tx:method name="get*" read-only="true"/><!-- other methods use the default transaction settings (see below) --><tx:method name=""/></tx:attributes></tx:advice><!-- ensure that the above transactional advice runs for any executionof an operation defined by the FooService interface --><aop:config><aop:pointcut id="fooServiceOperation" expression="execution(x.y.service.FooService.*(..))"/><aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation"/></aop:config><!-- don't forget the DataSource --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroymethod="close"><property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/><property name="url" value="jdbc:oracle:thin:@rj-t42:1521:elvis"/><property name="username" value="scott"/><property name="password" value="tiger"/></bean><!-- similarly, don't forget the PlatformTransactionManager --><bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean><!-- other <bean/> definitions here --></beans>
<pre name="code" class="html"><tx:advice id="defaultTxAdvice"><tx:attributes><tx:method name="get" read-only="true"/><tx:method name=""/></tx:attributes></tx:advice><tx:advice id="noTxAdvice"><tx:attributes><tx:method name="" propagation="NEVER"/></tx:attributes></tx:advice><!-- other transaction infrastructure beans such as a PlatformTransactionManageromitted... --></beans><tx:advice id="defaultTxAdvice"><tx:attributes><tx:method name="get" read-only="true"/><tx:method name=""/></tx:attributes></tx:advice><tx:advice id="noTxAdvice"><tx:attributes><tx:method name="" propagation="NEVER"/></tx:attributes></tx:advice><!-- other transaction infrastructure beans such as a PlatformTransactionManageromitted... --></beans>
注解方式的事务管理方式
<!-- from the file context.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:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!-- this is the service object that we want to make transactional --><bean id="fooService" class="x.y.service.DefaultFooService"/><!-- enable the configuration of transactional behavior based on annotations --><tx:annotation-driven transaction-manager="txManager"/><!-- aPlatformTransactionManager is still required --><bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!-- (this dependency is defined somewhere else) --><property name="dataSource" ref="dataSource"/></bean><!-- other <bean/> definitions here --></beans>
spring的tips
Spring recommends that you only annotate concrete classes (and methods of concrete classes)
with the @Transactional annotation, as opposed to annotating interfaces. You certainly can
place the @Transactional annotation on an interface (or an interface method), but this works
only as you would expect it to if you are using interface-based proxies. The fact that Java
annotations are not inherited from interfaces means that if you are using class-based proxies
( proxy-target-class="true") or the weaving-based aspect ( mode="aspectj"), then
the transaction settings are not recognized by the proxying and weaving infrastructure, and the
object will not be wrapped in a transactional proxy, which would be decidedly bad.
注解驱动的配置
@Transactional(readOnly = true)public class DefaultFooService implements FooService {public Foo getFoo(String fooName) {// do something}// these settings have precedence for this method@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)public void updateFoo(Foo foo) {// do something}}
@Transactional注解的配置说明
tip
Currently you cannot have explicit control over the name of a transaction, where name means the
transaction name that will be shown in a transaction monitor, if applicable (for example, WebLogic’s
transaction monitor), and in logging output. For declarative transactions, the transaction name is always
the fully-qualified class name + "." + method name of the transactionally-advised class. For example, if
the handlePayment(..) method of the BusinessService class started a transaction, the name of
the transaction would be: com.foo.BusinessService.handlePayment
@Transactional多事务管理
public class TransactionalService {@Transactional("order")public void setSomething(String name) { ... }@Transactional("account")public void doSomething() { ... }}
<tx:annotation-driven/><bean id="transactionManager1" class="org.springframework.jdbc.DataSourceTransactionManager">...<qualifier value="order"/></bean><bean id="transactionManager2" class="org.springframework.jdbc.DataSourceTransactionManager">...<qualifier value="account"/></bean>
客户化自己事务管理注解
@Target({ElementType.METHOD, ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Transactional("order")public @interface OrderTx {}@Target({ElementType.METHOD, ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Transactional("account")public @interface AccountTx {}
public class TransactionalService {@OrderTxpublic void setSomething(String name) { ... }@AccountTxpublic void doSomething() { ... }}
事务的传播
PROPAGATION_REQUIRED
When the propagation setting is PROPAGATION_REQUIRED, a logical transaction scope is created for
each method upon which the setting is applied. Each such logical transaction scope can determine
rollback-only status individually, with an outer transaction scope being logically independent from the
inner transaction scope. Of course, in case of standard PROPAGATION_REQUIRED behavior, all these
scopes will be mapped to the same physical transaction. So a rollback-only marker set in the inner
transaction scope does affect the outer transaction’s chance to actually commit (as you would expect
it to).
However, in the case where an inner transaction scope sets the rollback-only marker, the outer
transaction has not decided on the rollback itself, and so the rollback (silently triggered by the inner
transaction scope) is unexpected. A corresponding UnexpectedRollbackException is thrown at
that point. This is expected behavior so that the caller of a transaction can never be misled to assume
that a commit was performed when it really was not. So if an inner transaction (of which the outer caller
is not aware) silently marks a transaction as rollback-only, the outer caller still calls commit. The outer
caller needs to receive an UnexpectedRollbackException to indicate clearly that a rollback was
performed instead.
PROPAGATION_REQUIRES_NEW
PROPAGATION_REQUIRES_NEW, in contrast to PROPAGATION_REQUIRED, uses a completely
independent transaction for each affected transaction scope. In that case, the underlying physical
transactions are different and hence can commit or roll back independently, with an outer transaction
not affected by an inner transaction’s rollback status.
PROPAGATION_NESTED uses a single physical transaction with multiple savepoints that it can roll back
to. Such partial rollbacks allow an inner transaction scope to trigger a rollback for its scope, with the
outer transaction being able to continue the physical transaction despite some operations having been
rolled back. This setting is typically mapped onto JDBC savepoints, so will only work with JDBC resource
transactions. See Spring’s DataSourceTransactionManager.
通过实现ORdered接口
Here is the code for a simple profiling aspect discussed above. The ordering of advice is controlled
through the Ordered interface. For full details on advice ordering, see the section called “Advice
ordering”. .
package x.y;import org.aspectj.lang.ProceedingJoinPoint;import org.springframework.util.StopWatch;import org.springframework.core.Ordered;public class SimpleProfiler implements Ordered {private int order;// allows us to control the ordering of advicepublic int getOrder() {return this.order;}public void setOrder(int order) {this.order = order;}// this method is the around advicepublic Object profile(ProceedingJoinPoint call) throws Throwable {Object returnValue;StopWatch clock = new StopWatch(getClass().getName());try {clock.start(call.toShortString());returnValue = call.proceed();} finally {clock.stop();System.out.println(clock.prettyPrint());}return returnValue;}}
<?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:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><bean id="fooService" class="x.y.service.DefaultFooService"/><!-- this is the aspect --><bean id="profiler" class="x.y.SimpleProfiler"><!-- execute before the transactional advice (hence the lower order number) --><property name="order" value="1"/></bean><tx:annotation-driven transaction-manager="txManager" order="200"/><aop:config><!-- this advice will execute around the transactional advice --><aop:aspect id="profilingAspect" ref="profiler"><aop:pointcut id="serviceMethodWithReturnValue"expression="execution(!void x.y..Service.(..))"/><aop:around method="profile" pointcut-ref="serviceMethodWithReturnValue"/></aop:aspect></aop:config><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroymethod="close"><property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/><property name="url" value="jdbc:oracle:thin:@rj-t42:1521:elvis"/><property name="username" value="scott"/><property name="password" value="tiger"/></bean><bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean></beans>
<?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:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><bean id="fooService" class="x.y.service.DefaultFooService"/><!-- the profiling advice --><bean id="profiler" class="x.y.SimpleProfiler"><!-- execute before the transactional advice (hence the lower order number) --><property name="order" value="1"/></bean><aop:config><aop:pointcut id="entryPointMethod" expression="execution(* x.y..Service.(..))"/><!-- will execute after the profiling advice (c.f. the order attribute) --><aop:advisor advice-ref="txAdvice" pointcut-ref="entryPointMethod" order="2"/><!-- order value is higher than the profiling aspect --><aop:aspect id="profilingAspect" ref="profiler"><aop:pointcut id="serviceMethodWithReturnValue"expression="execution(!void x.y..Service.(..))"/><aop:around method="profile" pointcut-ref="serviceMethodWithReturnValue"/></aop:aspect></aop:config><tx:advice id="txAdvice" transaction-manager="txManager"><tx:attributes><tx:method name="get*" read-only="true"/><tx:method name="*"/></tx:attributes></tx:advice><!-- other <bean/> definitions such as a DataSource and a PlatformTransactionManagerhere --></beans>
- spring 声明式事务管理的配置方式
- spring声明式事务管理的配置
- Spring声明式事务管理的配置
- Spring声明式事务管理的配置详解
- Spring 声明式事务管理配置
- Spring的声明式事务管理
- Spring 的声明式事务管理
- spring的声明式事务管理
- Spring的声明式事务管理
- Spring的声明式事务管理
- Spring声明式事务管理、事务的传播行为xml配置
- spring使用TransactionProxy配置声明式的事务管理
- 基于配置的Spring声明式事务管理详解
- Spring声明式事务管理与配置详解
- spring声明式事务管理配置及说明
- Spring声明式事务管理与配置详解
- spring声明式事务管理配置及说明
- spring声明式事务管理配置及说明
- Linux ubuntu 14.04 环境下Qt creator中添加OpenCv2.4.9
- 最权威的身份证号码的正则表达式
- AutoInc ID问题
- 两名技术人员,历经8小时Piranha Games成功集成Xsolla
- HttpClient使用详解
- spring声明式事务管理的配置
- scikit-learn使用joblib持久化模型过程中的问题详解
- js获取当前时间戳
- ADO + SQL问题及解决方法
- 利用SqlHelper.cs实现Web程序与数据库的连接
- bmp图片中biXPelsPermeter biYPelsPermeter值的含义
- centos 系统挂载数据盘(两个盘的数据切换)
- 使用静态库时需要注意的事项(gcc下程序调用静态库编译命令:主文件必须在静态库前面!)
- jQuery学习笔记——$.grep()工具函数