在我们的Grails应用程序中,我们为每个租户使用一个数据库。每个数据库都包含所有域对象的数据。我们使用类似DomainClass.getDS().find()
的模式基于请求上下文切换数据库。
由于请求不知道要使用的事务管理器,因此事务无法开箱即用。 @Transactional
和withTransaction
什么都不做。
我已经实现了我自己的withTransaction()版本:
public static def withTransaction(Closure callable) {
new TransactionTemplate(getTM()).execute(callable as TransactionCallback)
}
getTM()根据请求上下文返回事务管理器,例如transactionManager_db0
。
这似乎适用于我的沙盒。这也会奏效:
我相信TDD,但除了最后一颗子弹外,我很难提供测试来验证代码。