此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Framework 6.2.10spring-doc.cadn.net.cn

声明式事务实现示例

请考虑以下接口及其附属实现。此示例使用FooBar类作为占位符,以便您可以专注于事务 使用而不关注特定领域模型。就本示例而言, 事实上,DefaultFooService类投掷UnsupportedOperationException每个实现方法的主体中的实例都是好的。该行为可让您看到 事务正在创建,然后回滚以响应UnsupportedOperationException实例。以下列表显示了FooService接口:spring-doc.cadn.net.cn

// the service interface that we want to make transactional

package x.y.service;

public interface FooService {

	Foo getFoo(String fooName);

	Foo getFoo(String fooName, String barName);

	void insertFoo(Foo foo);

	void updateFoo(Foo foo);

}
// the service interface that we want to make transactional

package x.y.service

interface FooService {

	fun getFoo(fooName: String): Foo

	fun getFoo(fooName: String, barName: String): Foo

	fun insertFoo(foo: Foo)

	fun updateFoo(foo: Foo)
}

以下示例显示了上述接口的实现:spring-doc.cadn.net.cn

package x.y.service;

public class DefaultFooService implements FooService {

	@Override
	public Foo getFoo(String fooName) {
		// ...
	}

	@Override
	public Foo getFoo(String fooName, String barName) {
		// ...
	}

	@Override
	public void insertFoo(Foo foo) {
		// ...
	}

	@Override
	public void updateFoo(Foo foo) {
		// ...
	}
}
package x.y.service

class DefaultFooService : FooService {

	override fun getFoo(fooName: String): Foo {
		// ...
	}

	override fun getFoo(fooName: String, barName: String): Foo {
		// ...
	}

	override fun insertFoo(foo: Foo) {
		// ...
	}

	override fun updateFoo(foo: Foo) {
		// ...
	}
}

假设FooService接口getFoo(String)getFoo(String, String),必须在具有只读的事务上下文中运行 语义学和其他方法insertFoo(Foo)updateFoo(Foo)必须 在具有读写语义的事务上下文中运行。以下内容 接下来的几段将详细解释配置:spring-doc.cadn.net.cn

<!-- 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/beans
		https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/tx
		https://www.springframework.org/schema/tx/spring-tx.xsd
		http://www.springframework.org/schema/aop
		https://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 execution
		of 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" destroy-method="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 TransactionManager -->
	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
	</bean>

	<!-- other <bean/> definitions here -->

</beans>

检查前面的配置。它假设你想创建一个服务对象, 这fooService豆,交易。要应用的事务语义被封装 在<tx:advice/>定义。这<tx:advice/>定义读作“所有方法 开头get在只读事务的上下文中运行,并且所有 其他方法是使用默认事务语义运行“。这transaction-manager属性的<tx:advice/>标记设置为TransactionManagerbean 的 bean(在本例中为txManager豆子)。spring-doc.cadn.net.cn

您可以省略transaction-manager属性 (<tx:advice/>) 如果 bean 名称的TransactionManager你想 wire in 有名称transactionManager.如果TransactionManagerbean 那 您要连接的有任何其他名称,则必须使用transaction-manager属性显式,如前面的示例所示。

<aop:config/>定义确保由txAdviceBean 在程序中的适当点运行。首先,定义一个 与FooService接口 (fooServiceOperation).然后,将切入点与txAdvice通过使用 顾问。结果表明,在执行fooServiceOperation, 由txAdvice运行。spring-doc.cadn.net.cn

<aop:pointcut/>元素是 AspectJ 切入点 表达。有关切入点的更多详细信息,请参阅AOP部分 Spring 中的表达式。spring-doc.cadn.net.cn

一个常见的要求是使整个服务层成为事务性的。最好的方法 这样做是为了更改切入点表达式以匹配 服务层。以下示例显示了如何执行此作:spring-doc.cadn.net.cn

<aop:config>
	<aop:pointcut id="fooServiceMethods" expression="execution(* x.y.service.*.*(..))"/>
	<aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceMethods"/>
</aop:config>
在前面的示例中,假设定义了所有服务接口 在x.y.service包。有关更多详细信息,请参阅 AOP 部分

现在我们已经分析了配置,你可能会问自己, “所有这些配置实际上有什么作用?”spring-doc.cadn.net.cn

前面显示的配置用于围绕对象创建事务代理 从fooServicebean 定义。代理配置了 事务性建议,以便在代理上调用适当的方法时, 事务是否启动、挂起、标记为只读等,具体取决于 与该方法关联的事务配置。考虑以下程序 该测试驱动前面显示的配置:spring-doc.cadn.net.cn

public final class Boot {

	public static void main(final String[] args) throws Exception {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml");
		FooService fooService = ctx.getBean(FooService.class);
		fooService.insertFoo(new Foo());
	}
}
import org.springframework.beans.factory.getBean

fun main() {
	val ctx = ClassPathXmlApplicationContext("context.xml")
	val fooService = ctx.getBean<FooService>("fooService")
	fooService.insertFoo(Foo())
}

运行前面程序的输出应类似于以下内容(Log4J 输出和堆栈跟踪从UnsupportedOperationExceptioninsertFoo(..)方法DefaultFooService为清楚起见,类已被截断):spring-doc.cadn.net.cn

<!-- the Spring container is starting up... -->
[AspectJInvocationContextExposingAdvisorAutoProxyCreator] - Creating implicit proxy for bean 'fooService' with 0 common interceptors and 1 specific interceptors

<!-- the DefaultFooService is actually proxied -->
[JdkDynamicAopProxy] - Creating JDK dynamic proxy for [x.y.service.DefaultFooService]

<!-- ... the insertFoo(..) method is now being invoked on the proxy -->
[TransactionInterceptor] - Getting transaction for x.y.service.FooService.insertFoo

<!-- the transactional advice kicks in here... -->
[DataSourceTransactionManager] - Creating new transaction with name [x.y.service.FooService.insertFoo]
[DataSourceTransactionManager] - Acquired Connection [org.apache.commons.dbcp.PoolableConnection@a53de4] for JDBC transaction

<!-- the insertFoo(..) method from DefaultFooService throws an exception... -->
[RuleBasedTransactionAttribute] - Applying rules to determine whether transaction should rollback on java.lang.UnsupportedOperationException
[TransactionInterceptor] - Invoking rollback for transaction on x.y.service.FooService.insertFoo due to throwable [java.lang.UnsupportedOperationException]

<!-- and the transaction is rolled back (by default, RuntimeException instances cause rollback) -->
[DataSourceTransactionManager] - Rolling back JDBC transaction on Connection [org.apache.commons.dbcp.PoolableConnection@a53de4]
[DataSourceTransactionManager] - Releasing JDBC Connection after transaction
[DataSourceUtils] - Returning JDBC Connection to DataSource

Exception in thread "main" java.lang.UnsupportedOperationException at x.y.service.DefaultFooService.insertFoo(DefaultFooService.java:14)
<!-- AOP infrastructure stack trace elements removed for clarity -->
at $Proxy0.insertFoo(Unknown Source)
at Boot.main(Boot.java:11)

要使用响应式事务管理,代码必须使用响应式类型。spring-doc.cadn.net.cn

Spring Framework 使用ReactiveAdapterRegistry确定方法 返回类型为响应式。

以下列表显示了以前使用的FooService但 这次代码使用响应式类型:spring-doc.cadn.net.cn

// the reactive service interface that we want to make transactional

package x.y.service;

public interface FooService {

	Flux<Foo> getFoo(String fooName);

	Publisher<Foo> getFoo(String fooName, String barName);

	Mono<Void> insertFoo(Foo foo);

	Mono<Void> updateFoo(Foo foo);

}
// the reactive service interface that we want to make transactional

package x.y.service

interface FooService {

	fun getFoo(fooName: String): Flow<Foo>

	fun getFoo(fooName: String, barName: String): Publisher<Foo>

	fun insertFoo(foo: Foo) : Mono<Void>

	fun updateFoo(foo: Foo) : Mono<Void>
}

以下示例显示了上述接口的实现:spring-doc.cadn.net.cn

package x.y.service;

public class DefaultFooService implements FooService {

	@Override
	public Flux<Foo> getFoo(String fooName) {
		// ...
	}

	@Override
	public Publisher<Foo> getFoo(String fooName, String barName) {
		// ...
	}

	@Override
	public Mono<Void> insertFoo(Foo foo) {
		// ...
	}

	@Override
	public Mono<Void> updateFoo(Foo foo) {
		// ...
	}
}
package x.y.service

class DefaultFooService : FooService {

	override fun getFoo(fooName: String): Flow<Foo> {
		// ...
	}

	override fun getFoo(fooName: String, barName: String): Publisher<Foo> {
		// ...
	}

	override fun insertFoo(foo: Foo): Mono<Void> {
		// ...
	}

	override fun updateFoo(foo: Foo): Mono<Void> {
		// ...
	}
}

命令式和响应式事务管理共享相同的事务语义 边界和事务属性定义。命令式之间的主要区别 反应易是后者的递延性质。TransactionInterceptor使用事务运算符修饰返回的响应式类型以开始和清理 交易。因此,调用事务响应式方法会延迟实际的 事务管理设置为激活响应式处理的订阅类型 类型。spring-doc.cadn.net.cn

响应式事务管理的另一个方面与数据转义有关,即 编程模型的自然结果。spring-doc.cadn.net.cn

命令式事务的方法返回值是从事务性方法返回的 成功终止方法后,以便部分计算的结果不会逃逸 方法闭包。spring-doc.cadn.net.cn

响应式事务方法返回一个响应式包装器类型,该类型表示 计算序列以及开始和完成计算的承诺。spring-doc.cadn.net.cn

一个Publisher可以在事务进行时发出数据,但不一定完成。 因此,依赖于成功完成整个事务的方法需要 以确保调用代码中的完成和缓冲结果。spring-doc.cadn.net.cn