1 回答

TA貢獻1806條經驗 獲得超5個贊
由于您的應用程序使用 spring,您可以嘗試通過以下幾種方法之一來完成此操作:
春季AOP
在這種方法中,您編寫一條建議,要求 spring 將其應用于特定方法。如果您的方法使用@Transactional注釋,您可以在事務開始后立即將建議應用于那些方法。
擴展 TransactionManager 實現
讓我們假設您的交易正在使用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
}
}
}
現在只需配置您的 JPA 環境即可使用您的自定義JpaTransactionManager類。
可能還有其他的,但這是我想到的兩個。
添加回答
舉報