使用ssm过程中,遇到了service自调的问题,于是研究了this与AOP代理两种方法。
测试类:
public interface IService {
void a();
void b();
}
@Service("service")
public class ServiceImpl implements IService{
@Override
@Transactional(propagation=Propagation.REQUIRED)
public void a() {
// this.b();
((IService) AopContext.currentProxy()).b();
}
@Override
@Transactional(propagation=Propagation.REQUIRES_NEW)
public void b() { }
}
this.b()的方式:
如果 a 方法使用 this.b( ) 的方式调用方法,b 事务是不会增强的,Spring 文档中说过,只有外部调动才会增强, this 指的是当前对象,而不是 proxy 对象,所以事务不会增强。这时候 b 方法上的 @Transactional(propagation = Propagation.REQUIRES_NEW) 不起作用,也就是说这时候 b 方法与 a 方法的事务定义是一样的,这一点可以从日志中看出:
Returning cached instance of singleton bean 'service'
Adding transactional method 'ServiceImpl.a' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
Returning cached instance of singleton bean 'transactionManager'
//创建新事务(a事务)
Creating new transaction with name [com.llangzh.services.impls.ServiceImpl.a]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
incremented pending_acquires: 1
incremented pending_acquires: 2
incremented pending_acquires: 3
com.mchange.v2.resourcepool.BasicResourcePool@5b1669c0 config: [start -> 3; min -> 3; max -> 15; inc -> 3; num_acq_attempts -> 30; acq_attempt_delay -> 1000; check_idle_resources_delay -> 0; mox_resource_age -> 0; max_idle_time -> 0; excess_max_idle_time -> 0; destroy_unreturned_resc_time -> 0; expiration_enforcement_delay -> 0; break_on_acquisition_failure -> false; debug_store_checkout_exceptions -> false]
Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1hge15xa2dme3vb1ky7nif|71d44a3, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hge15xa2dme3vb1ky7nif|71d44a3, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql://localhost:3306/ssm, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 15, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]
acquire test -- pool size: 0; target_pool_size: 3; desired target? 1
awaitAvailable(): [unknown]
trace com.mchange.v2.resourcepool.BasicResourcePool@5b1669c0 [managed: 0, unused: 0, excluded: 0]
com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager@4360c56.acquireResource() returning.
com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager@4360c56.acquireResource() returning.
com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager@4360c56.acquireResource() returning.
trace com.mchange.v2.resourcepool.BasicResourcePool@5b1669c0 [managed: 1, unused: 1, excluded: 0]
decremented pending_acquires: 2
trace com.mchange.v2.resourcepool.BasicResourcePool@5b1669c0 [managed: 2, unused: 1, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@7a21477a)
decremented pending_acquires: 1
trace com.mchange.v2.resourcepool.BasicResourcePool@5b1669c0 [managed: 3, unused: 2, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@7a21477a)
decremented pending_acquires: 0
trace com.mchange.v2.resourcepool.BasicResourcePool@5b1669c0 [managed: 3, unused: 2, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@7a21477a)
Acquired Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@7d0b7e3c] for JDBC transaction
Switching JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@7d0b7e3c] to manual commit
Initiating transaction commit //提交事务(a事务)
Committing JDBC transaction on Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@7d0b7e3c]
//释放资源(a事务)
Releasing JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@7d0b7e3c] after transaction
Returning JDBC Connection to DataSource
trace com.mchange.v2.resourcepool.BasicResourcePool@5b1669c0 [managed: 3, unused: 2, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@7a21477a)
我们可以看到事务切面只为 a 创建了事务,所有操作都是对 a 进行的,并没有对 b 进行事务上的处理,这就证明了通过
this.b() 的方式不会对 b 进行事务增强。
AOP代理方式 ((IService) AopContext.currentProxy()).b():
通过 ThreadLocal 暴露 AOP 代理对象,在Spring配置文件中添加配置:
<!-- 使用AOP代理 -->
<aop:aspectj-autoproxy expose-proxy="true" />
测试看日志:
Returning cached instance of singleton bean 'service'
Adding transactional method 'ServiceImpl.a' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
Returning cached instance of singleton bean 'transactionManager'
//创建新事务(a事务)
Creating new transaction with name [com.llangzh.services.impls.ServiceImpl.a]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
......
Acquired Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@7d0b7e3c] for JDBC transaction
Switching JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@7d0b7e3c] to manual commit
//悬浮当前事务(a事务),创建新事务(b事务)
Suspending current transaction, creating new transaction with name [com.llangzh.services.impls.ServiceImpl.b]
Acquired Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@235ecd9f] for JDBC transaction
Switching JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@235ecd9f] to manual commit
Initiating transaction commit //b事务提交
Committing JDBC transaction on Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@235ecd9f]
Releasing JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@235ecd9f] after transaction
Returning JDBC Connection to DataSource
Resuming suspended transaction after completion of inner transaction //内部事务(b事务)结束后,继续悬浮的事务(a事务)
Initiating transaction commit //a事务提交
Committing JDBC transaction on Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@7d0b7e3c]
Releasing JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@7d0b7e3c] after transaction
Returning JDBC Connection to DataSource
通过日志可以看到事务切面为 b 创建了事务,并且在 b 事务执行过程中,a 事务一直处于悬浮状态,只有当 b 事务处理完成后,才会继续 a 事务。