@PostRemove退出事务了吗? [英] Is @PostRemove out of transaction?

查看:185
本文介绍了@PostRemove退出事务了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从规范中发现了以下信息.但是对于我不是英国人的我来说还不够清楚.

I found following information from the spec. But it's not clear enough for me who is not an english native.

在使实体成为持久性或删除实体后,将为该实体调用PostPersistPostRemove回调方法.这些回调还将在这些操作所级联到的所有实体上被调用. PostPersistPostRemove方法将分别在数据库插入和删除操作之后调用.这些数据库操作可以在调用持久,合并或删除操作之后直接发生,也可以在发生刷新操作后立即发生(可能在事务结束时).生成的主键值在PostPersist方法中可用.

The PostPersist and PostRemove callback methods are invoked for an entity after the entity has been made persistent or removed. These callbacks will also be invoked on all entities to which these operations are cascaded. The PostPersist and PostRemove methods will be invoked after the database insert and delete operations respectively. These database operations may occur directly after the persist, merge, or remove operations have been invoked or they may occur directly after a flush operation has occurred (which may be at the end of the transaction). Generated primary key values are available in the PostPersist method.

我的问题是在@PostRemove之后可以回滚任何与交易相关的工作吗?

My question is any transaction related jobs can be rolled back after @PostRemove?

假设我的实体删除了@PostRemove

class MyEntity {

    @PostRemove
    private void onPostRemove() {
        // delete offline files related to this entity
        // not restorable!
    }
}

这些脱机文件是否有可能从存储中删除,而实体仍留在数据库中? (通过回滚?)

Is it possible that those offline files deleted from the storage and the entity still left in the database? (by rollback?)

推荐答案

是的,回滚后,您的文件可能会被删除,entent仍保留在db中. @PostRemove是交易中的.

Yes, it is possible that your files are deleted and your entites are still left in db after a rollback. @PostRemove is in transaction.

如果要绝对确保仅当事务成功完成时才删除文件,则应在commit()成功后不使用回调方法删除文件.但是,如果您还需要确保仅当文件被删除时才删除实体,那么您就遇到了问题.您需要一种访问文件系统的事务方式.

If you want to be absolutely sure that your files are deleted if and only if the transaction is successfully completed then you should delete the files after the commit() succeeds not using the callback methods. But if you also need to be sure that the entity is removed if and only if the file is deleted, then you have a problem. You need a transactional way of accessing the file system.

对于一个简单的解决方案,在db-transaction期间将文件移动到to_be_deleted文件夹中.因此,您可以使用回调方法.当commit()成功时,文件最终被删除,并在失败时恢复.

For a simple solution move your files into a to_be_deleted-folder during the db-transaction. Therefore you can use the callback methods. The files are finally deleted when commit() succeeds and restored on failure.

如果您想进一步说明它,并且您的应用程序在java EE容器中运行,那么您可能需要查看 CDI事件,甚至是 jca适配器.如果您使用的是Spring,则可以注册TransactionSynchronizationAdapter,请参见此答案.

If you want to elaborate it a bit more and your application is running in a java EE container, then you might want to look at CDI events or even at a jca adapter. If you are using Spring you can register a TransactionSynchronizationAdapter see this answer.

这篇关于@PostRemove退出事务了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆