Grails 2.5 afterUpdate - 它在事务生命周期中被称为什么点?
Grails 2.5 afterUpdate - what point in the transaction lifecycle is it called?
如果我们在 grails 中的域对象(例如我们的 Session 对象)上添加 afterUpdate 事件代码:
- 是更新提交后调用,刷新后调用,还是其他?
- 如果更新失败(比如约束,或者乐观锁失败),after事件还会被调用吗?
- afterUpdate 会和更新在同一个事务中吗?
- 执行更新的服务方法的提交是否会等到 afterUpdate 方法完成,如果是这样,有什么办法解决这个问题(除了创建新线程)?
我们在多个 tomcat 上有许多 grails 应用程序实例 运行。每个都有一个会话到期石英作业来终止我们的会话(域对象)
作业基本上说 getAllSession 和 lastUpdated > xxx,然后循环调用 session.close(Session.Expired)
Session.close 只是将 session.status 设置为已过期。
理论上,同一个会话可以同时关闭两次在两台服务器上购买作业运行,但这并不重要(目前)
现在我们要自动为会话过期(或终止)的客户提现。提现过程需要调用外部支付系统,这可能需要长达 1 分钟,并且可能会失败(但不应阻止关闭会话或 'lock' 其他会话)
如果我们在会话域对象上使用 afterUpdate,我们可以检查 session.status,并在交易之外或在另一个线程(例如使用执行器)中触发提现。但这是非常冒险的——因为我们不知道确切的行为。例如。如果更新失败,它还会尝试执行 afterUpdate 调用吗?我们假设是这样,因为我们猜测提交要到以后才会发生。
另一个未知的是调用保存和提交如何与乐观锁定一起工作。例如。如果您调用 save(flush=true),并且没有返回错误,您是否保证提交会工作(除非数据库崩溃),或者是否存在可能失败的情况?
- Is it called after the update has been committed, or after it is
flushed, or other?
- 更新完成后,事务还没有提交。所以如果afterUpdate内部发生异常,事务会被回滚
- If the update failed (e.g. constraint, or optimistic lock fail), will the after event still be called?
- 没有
- Will after Update be in the same transaction as the update?
- 是
- Will the commit of the service method which did the update wait till the afterUpdate method is finished, and, if so, is there any way
round this (except creating a new thread)?
- 没有捷径
如果我们在 grails 中的域对象(例如我们的 Session 对象)上添加 afterUpdate 事件代码:
- 是更新提交后调用,刷新后调用,还是其他?
- 如果更新失败(比如约束,或者乐观锁失败),after事件还会被调用吗?
- afterUpdate 会和更新在同一个事务中吗?
- 执行更新的服务方法的提交是否会等到 afterUpdate 方法完成,如果是这样,有什么办法解决这个问题(除了创建新线程)?
我们在多个 tomcat 上有许多 grails 应用程序实例 运行。每个都有一个会话到期石英作业来终止我们的会话(域对象)
作业基本上说 getAllSession 和 lastUpdated > xxx,然后循环调用 session.close(Session.Expired)
Session.close 只是将 session.status 设置为已过期。
理论上,同一个会话可以同时关闭两次在两台服务器上购买作业运行,但这并不重要(目前)
现在我们要自动为会话过期(或终止)的客户提现。提现过程需要调用外部支付系统,这可能需要长达 1 分钟,并且可能会失败(但不应阻止关闭会话或 'lock' 其他会话)
如果我们在会话域对象上使用 afterUpdate,我们可以检查 session.status,并在交易之外或在另一个线程(例如使用执行器)中触发提现。但这是非常冒险的——因为我们不知道确切的行为。例如。如果更新失败,它还会尝试执行 afterUpdate 调用吗?我们假设是这样,因为我们猜测提交要到以后才会发生。
另一个未知的是调用保存和提交如何与乐观锁定一起工作。例如。如果您调用 save(flush=true),并且没有返回错误,您是否保证提交会工作(除非数据库崩溃),或者是否存在可能失败的情况?
- Is it called after the update has been committed, or after it is flushed, or other?
- 更新完成后,事务还没有提交。所以如果afterUpdate内部发生异常,事务会被回滚
- If the update failed (e.g. constraint, or optimistic lock fail), will the after event still be called?
- 没有
- Will after Update be in the same transaction as the update?
- 是
- Will the commit of the service method which did the update wait till the afterUpdate method is finished, and, if so, is there any way round this (except creating a new thread)?
- 没有捷径