I use following declarative Spring transaction:
我使用以下声明式Spring事务:
<!-- Declare a transaction manager-->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory" />
<!-- enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="transactionManager" mode="proxy" proxy-target-class="true"/>
Here is the DAO:
这是DAO:
@Repository
@Transactional(readOnly = true, propagation=Propagation.REQUIRES_NEW )
@Scope("prototype")
public class Xdao{
public Object getValues(){
.....
}
}
@Service
@Scope("prototype")
public class Xservice{
private Xdao xdao;
public Object getx(){
xdao.getValues();//here I want to know whether the transaction started is
//committed or rollback by aop. Is it possible somehow? I don't want to include that code
//in any service or dao layer.
.........
}
@Autowired
public void setXdao(Xdao xdao){
this.xdao=xdao;
}
}
I want to know about transaction status i.e transaction is committed or rolled back. I need it for logging.
我想知道事务状态,即事务已提交或回滚。我需要它来记录。
2 个解决方案
#1
18
If transaction is in scope you can get TransactionStatus
from TransactionAspectSupport.currentTransactionStatus()
. For example:
如果事务在范围内,您可以从TransactionAspectSupport.currentTransactionStatus()获取TransactionStatus。例如:
if (TransactionSynchronizationManager.isActualTransactionActive()) {
TransactionStatus status = TransactionAspectSupport.currentTransactionStatus();
...
}
But this will not work after transaction is completed.
但这在交易完成后无效。
You could add a TransactionSynchronization
and implement afterCompletion(int status)
to log the status or store it in a ThreadLocal
variable for later usage.
您可以添加TransactionSynchronization并实现afterCompletion(int status)以记录状态或将其存储在ThreadLocal变量中以供以后使用。
public class LogTransactionSynchronization extends TransactionSynchronizationAdapter {
@Override
public afterCompletion(int status) {
// log the status or store it for later usage
}
}
#2
3
Adding the following to your log4j.properties will enable transaction status logging,
将以下内容添加到log4j.properties将启用事务状态日志记录,
log4j.logger.org.hibernate.transaction=DEBUG,R
log4j.logger.org.springframework.transaction=DEBUG,R
#1
18
If transaction is in scope you can get TransactionStatus
from TransactionAspectSupport.currentTransactionStatus()
. For example:
如果事务在范围内,您可以从TransactionAspectSupport.currentTransactionStatus()获取TransactionStatus。例如:
if (TransactionSynchronizationManager.isActualTransactionActive()) {
TransactionStatus status = TransactionAspectSupport.currentTransactionStatus();
...
}
But this will not work after transaction is completed.
但这在交易完成后无效。
You could add a TransactionSynchronization
and implement afterCompletion(int status)
to log the status or store it in a ThreadLocal
variable for later usage.
您可以添加TransactionSynchronization并实现afterCompletion(int status)以记录状态或将其存储在ThreadLocal变量中以供以后使用。
public class LogTransactionSynchronization extends TransactionSynchronizationAdapter {
@Override
public afterCompletion(int status) {
// log the status or store it for later usage
}
}
#2
3
Adding the following to your log4j.properties will enable transaction status logging,
将以下内容添加到log4j.properties将启用事务状态日志记录,
log4j.logger.org.hibernate.transaction=DEBUG,R
log4j.logger.org.springframework.transaction=DEBUG,R