在春天,如果我有:
ServiceA.serviceA() -> ServiceB.serviceB() -> ServiceC.serviceC() ->ServiceD.serviceD()
哪里ServiceD.serviceD()可以抛出运行时异常:MyRuntimeException,它被传播回ServiceA.serviceAcatch块.我穿上哪种服务是否重要@Transactional(noRollbackFor=[MyRuntimeException.class])?
将它放在任何服务上有什么区别吗?
注意:我的所有服务都标记为@Transactional
我有一个用@Transactional 和另一个自定义注释@Custom 注释的方法。此自定义注释包含在建议中。操作顺序如下:
1.Transactional method gets called
2.Sees @Transactional and @Custom
3.As @Custom is intercepted by around advice, it first executes code before invocation of method
4.invocationContext.proceed()
5.Transaction gets created
6.Actual method runs
7.Back to around advice and executes code after method invocation
Run Code Online (Sandbox Code Playgroud)
我想在调用建议之前创建事务。像下面这样:
1.Transactional method gets called
2.Sees @Transactional and @Custom
3.Transaction gets created (propagate this transaction to @Custom)
4.As @Custom is intercepted by around advice, it first executes code before invocation of method
5.invocationContext.proceed()
6.Actual method runs
7.Back …Run Code Online (Sandbox Code Playgroud) 我正在使用Spring JDBC进行注释驱动的事务管理.
我想让Spring抛出异常,因为我错误地用@Transactional注释了一个插入/更新/删除的服务方法.
默认情况下,即使不在事务中,也可以插入/更新/删除数据.
我无法阻止事务在 RuntimeException 之后回滚。我的环境是在 Websphere 8.0 上运行的 Spring 4.1 + Hibernate 3.6 + JTA (WebSphereUowTransactionManager)。
首先,一个行为符合预期的简单案例。由于我捕获了 RuntimeException,事务提交并成功创建了新资源。
@Service("fooService")
public class FooServiceImpl implements IFooService {
@Transactional
@Override
public void doStuff(Resource res){
authService.createResource(res, "ADMIN");
try {
throw new RuntimeException("SOMETHING");
} catch (RuntimeException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
下一个也OK。我声明了noRollbackFor并让我们提交事务:
@Transactional(noRollbackFor=RuntimeException.class)
@Override
public void doStuff2(Resource res){
authService.createResource(res, "ADMIN");
throw new RuntimeException("SOMETHING");
}
Run Code Online (Sandbox Code Playgroud)
最后是有问题的。不同之处在于,在这种情况下,第二次调用引发了异常authService.createResource。仅供参考,authService.createResource仅标记为@Transactional,因此默认传播配置适用,它应该加入调用服务的事务。
@Transactional(noRollbackFor=RuntimeException.class)
@Override
public void doStuff12(Resource res){
authService.createResource(res, "ADMIN");
try{
res.setName("EXISTING-RESOURCE");
authService.createResource(res, "ADMIN");
}catch(RuntimeException e){ …Run Code Online (Sandbox Code Playgroud) 有没有办法在xml中替换以下Spring配置:
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" no-rollback-for="MyNoCausingRollbackException" />
</tx:attributes>
Run Code Online (Sandbox Code Playgroud)
带注释或 java 配置?
我的目标是以这种方式配置每个事务行为,因此抛出MyNoCausingRollbackException带有注释的方法@Transactional不会回滚当前事务。我想避免每次Transactional(noRollbackFor = ResourceNotFoundException.class)在可以抛出此类异常的方法上使用时定义此类行为。
TransactionManagementType.CONTAINER 和 TransactionManagementType.BEAN 有什么区别
因为我在我所有的 EJB 中使用 TransactionManagementType.CONTAINER 并且当使用多个数据库实例时,它会抛出一个错误,如果我将其更改为 TransactionManagementType.BEAN
我想知道如果我将其更改为 TransactionManagementType.BEAN 有什么优点和缺点以及如何影响它
ERROR:
Error updating database. Cause: java.sql.SQLException: javax.resource.ResourceException:
IJ000457: Unchecked throwable in managedConnectionReconnected() cl=org.jboss.jca.core.
connectionmanager.listener.TxConnectionListener@680f2be0[state=NORMAL managed
connection=org.jboss.jca.adapters.jdbc.local.LocalManagedConnection@7ba33a94 connection
handles=0 lastReturned=1495691675021 lastValidated=1495690817487
lastCheckedOut=1495691675018 trackByTx=false pool=org.jboss.jca.core.connectionmanager.
pool.strategy.OnePool@efd42c4 mcp=SemaphoreConcurrentLinkedQueueManagedConnectionPool
@71656eec[pool=FAQuery] xaResource=LocalXAResourceImpl@4c786e85
[connectionListener=680f2be0 connectionManager=5c3b98bc warned=false
currentXid=null productName=Oracle productVersion=Oracle Database 12c
Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options
jndiName=java:/FAQuery] txSync=null]
Run Code Online (Sandbox Code Playgroud) transactionmanager spring-transactions ejb-3.0 spring-mybatis
我有一个小弹簧启动应用程序spring-boot-starter-web,spring-boot-starter-data-jpa以及postgresql为依赖关系。
我能够使用@Transactional注释并使用 JPA 来获取实体并将其保存到数据库中。但是,如果我要通过注册同步来添加afterCommit/afterCompletion钩子,它会IllegalStateException说Transaction synchronization is not active.
TransactionSynchronizationManager.registerSynchronization(
new TransactionSynchronizationAdapter() {
@Override
public void afterCommit() {
//this doesn't get called
log.info("do something here");
}
});
Run Code Online (Sandbox Code Playgroud)
这样做TransactionSynchronizationManager.initSynchronization();可以消除错误,但不会调用钩子(例如:afterCommit即使事务已提交,也不会调用钩子。)
关于如何调试这个的任何线索?
我有以下代码:
/**
* Restored deleted partner
*/
@Transactional
public void restorePartnerById(Integer id){
// logic
}
Run Code Online (Sandbox Code Playgroud)
当我使方法最终
/**
* Restored deleted partner
*/
@Transactional
public final void restorePartnerById(Integer id){
// logic
}
Run Code Online (Sandbox Code Playgroud)
我收到一个编译错误,说明:
注释的方法
@Transactional必须是可覆盖的
我四处寻找,但我无法理解为什么它必须能够覆盖,为什么该方法必须能够覆盖?
java overriding transactional spring-transactions spring-boot
我正在尝试使用 Spring boot 和 Spring data JPA 创建一个 Restful API 来执行 CRUD 操作。数据库将是Oracle关系数据库。现在进行并发访问,如果我们仅使用@Transactional的Spring事务,是否可以满足我们并发CRUD操作的目的。
我看到有JPA乐观和悲观锁定策略版本专栏。我的具体问题是,对于并发 CRUD 操作,我们是否需要 Spring 事务和 JPA 锁定策略?或者只相应地配置 Spring 事务就足够了?
concurrency jpa spring-transactions spring-data-jpa spring-boot
是否有可能将声明式交易管理(通过@Transactional)与@KafkaListener注释方法一起使用?例如,我想使用它来为每个侦听器定义单独的 tx 超时。我的设置如下:
交易管理器:
@Bean
@ConditionalOnBean(value = {HibernateTransactionManager.class})
public ChainedKafkaTransactionManager<Object, Object> chainedHibernateTm(KafkaTransactionManager<String, String> kafkaTransactionManager,
org.springframework.orm.hibernate5.HibernateTransactionManager hibernateTransactionManager) {
return new ChainedKafkaTransactionManager<>(
kafkaTransactionManager,
hibernateTransactionManager);
}
Run Code Online (Sandbox Code Playgroud)
卡夫卡监听器:
@KafkaListener(topic = "my_topic")
@Transactional(timeout = 5)
public void handleMessage(SomeMessage message){
}
Run Code Online (Sandbox Code Playgroud)
问题是 - KafkaMessageListenerContainer 在调用此类方法之前创建自己的事务 - 它使用自己的 TransactionTemplate:
@Nullable
private TransactionTemplate determineTransactionTemplate() {
return this.transactionManager != null
? new TransactionTemplate(this.transactionManager)
: null;
}
Run Code Online (Sandbox Code Playgroud)
未使用 TransactionInterceptor。那么如何为具体的 @KafkaListener 方法设置特定的 tx 超时呢?
spring ×6
java ×5
spring-boot ×4
concurrency ×1
ejb-3.0 ×1
hibernate ×1
jpa ×1
jta ×1
overriding ×1
spring-aop ×1
spring-jdbc ×1
spring-kafka ×1
websphere ×1