1 回答

TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超5個(gè)贊
由于您的應(yīng)用程序使用 spring,您可以嘗試通過(guò)以下幾種方法之一來(lái)完成此操作:
春季AOP
在這種方法中,您編寫一條建議,要求 spring 將其應(yīng)用于特定方法。如果您的方法使用@Transactional注釋,您可以在事務(wù)開(kāi)始后立即將建議應(yīng)用于那些方法。
擴(kuò)展 TransactionManager 實(shí)現(xiàn)
讓我們假設(shè)您的交易正在使用JpaTransactionManager.
public class SecurityPolicyInjectingJpaTransactionManager extends JpaTransactionManager {
@Autowired
private EntityManager entityManager;
// constructors
@Override
protected void prepareSynchronization(DefaultTransactionStatus status, TransactionDefinition definition) {
super.prepareSynchronization(status, definition);
if (status.isNewTransaction()) {
// Use entityManager to execute your database policy param/values
// I would suggest you also register an after-completion callback synchronization
// This after-completion would clear all the policy param/values
// regardless of whether the transaction succeeded or failed
// since this happens just before it gets returned to the connection pool
}
}
}
現(xiàn)在只需配置您的 JPA 環(huán)境即可使用您的自定義JpaTransactionManager類。
可能還有其他的,但這是我想到的兩個(gè)。
添加回答
舉報(bào)