gpt4 book ai didi

java - Spring中当前线程禁止@Transactional

转载 作者:行者123 更新时间:2023-12-02 10:39:46 26 4
gpt4 key购买 nike

我正在寻找下一个问题的解决方案。我使用 Spring 并且我有一些不应该被阻塞的执行器线程(至少读/写数据库)。有几个任务可以通过这个执行器执行,我不知道哪些可以使用 @Transactional 方法或任何其他方式来访问数据库。我想禁止在执行者线程中打开新事务,该怎么做?

小例子

@Component
public class Service {

public void execute() {
System.out.println("I don't need transaction to execute");
}
}

@Component
public class Service2 {

@Transactional
public void execute() {
System.out.println("I've opened new transaction!");
}
}

@Component
public class NonTransactionalExecutor {

@Autowired
private ThreadPoolExecutor threadPoolExecutor;

@Autowired
private Service service;

@@Autowired
private Service2 service2;

public void doInExecutor() {
threadPoolExecutor.execute(new Runnable() {
@Override
public void run() {
// Is there anything I could do to get exception on service2.execute?
service.execute();
service2.execute();
}
});
}
}

附注在现实世界中,当方法用 @Transactional 注释时,它会尝试使用数据库中当前的 TransactionManager 打开新事务(我使用的是 Postgres)。我想要获得异常而不是数据库读/写。

   java.lang.Thread.State: RUNNABLE
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:171)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at org.postgresql.core.VisibleBufferedInputStream.readMore(VisibleBufferedInputStream.java:140)
at org.postgresql.core.VisibleBufferedInputStream.ensureBytes(VisibleBufferedInputStream.java:109)
at org.postgresql.core.VisibleBufferedInputStream.read(VisibleBufferedInputStream.java:67)
at org.postgresql.core.PGStream.receiveChar(PGStream.java:293)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1936)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:306)
- locked <4cd92d89> (a org.postgresql.core.v3.QueryExecutorImpl)
at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:441)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:365)
at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:307)
at org.postgresql.jdbc.PgStatement.executeCachedSql(PgStatement.java:293)
at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:270)
at org.postgresql.jdbc.PgStatement.executeQuery(PgStatement.java:224)
at com.mchange.v2.c3p0.impl.DefaultConnectionTester.activeCheckConnection(DefaultConnectionTester.java:286)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.testPooledConnection(C3P0PooledConnectionPool.java:510)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.testPooledConnection(C3P0PooledConnectionPool.java:464)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.refurbishResourceOnCheckout(C3P0PooledConnectionPool.java:339)
- locked <73ed3d84> (a java.lang.Object)
at com.mchange.v2.resourcepool.BasicResourcePool.attemptRefurbishResourceOnCheckout(BasicResourcePool.java:1733)
at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:558)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutAndMarkConnectionInUse(C3P0PooledConnectionPool.java:758)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:685)
at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:140)
at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:139)
at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:380)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:228)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:171)
at org.hibernate.internal.SessionImpl.connection(SessionImpl.java:450)
at org.springframework.orm.hibernate4.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:450)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:373)
at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:427)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:276)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)

最佳答案

您可能可以使用装饰器模式来实现此目的。实现您自己的 PlatformTransactionManager 并将您当前的管理器作为委托(delegate)传递给它。在 getTransaction 中,如果当前线程不是您所期望的,则抛出异常。

public class MyTransactionManager implements PlatformTransactionManager {
private final PlatformTransactionManager delegate;

public MyTransactionManager(PlatformTransactionManager delegate) {
this.delegate = delegate;
}

@Override
public void commit(TransactionStatus status) {
delegate.commit(status);
}

@Override
public TransactionStatus getTransaction(TransactionDefinition definition) {
if (Thread.getCurrentThread() == /*something*/) {
return delegate.getTransaction(definition);
}
else {
throw new RuntimeException();
}
}

@Override
public void rollback(TransactionStatus status) {
delegate.rollback(status);
}
}

关于java - Spring中当前线程禁止@Transactional,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53008940/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com