在实体管理器中调用获取连接的本机语句
我的公司正在使用数据库 ONINSERT、ONUPDATE、ONDELETE 触发器来记录更改,还有一种方法可以通过调用设置数据库会话参数的存储过程来模拟另一个数据库用户。然后,对该连接所做的任何更改都会自动分配给该用户。我知道这是一个糟糕的设计,但我无法改变这一点。
现在我需要确保在实体管理器将更改与数据库同步之前始终调用此存储过程,并且每次模拟的用户都不同。我尝试简单地通过将存储过程调用包装在无状态 EJB 中并在对实体进行任何更改之前调用该 EJB 来实现此目的,但这在某些情况下不起作用 - 它的行为就像从未调用该过程一样。我的猜测是,该过程是在与发布更改本身不同的连接中调用的,尽管这应该发生在单个事务中。
有什么干净的方法可以做到这一点吗?
My company is using database ONINSERT, ONUPDATE, ONDELETE triggers for logging changes and there is also a way to impersonate another database user by calling a stored procedure which sets a database session parameter. Then any changes made with that connection are automatically assigned to that user. I know it is a bad design but I cannot change this.
Now I need to make sure this stored procedure is always called before entity manager synchronizes changes with database and the user to impersonate is different each time. I tried simply doing it by simply wrapping the stored procedure call in a stateless EJB and calling that EJB before I make any changes to entities but this does not work in some cases - it acts like the procedure was never called. My guess is that the procedure was called in a different connection than the changes themselves were posted although this was supposed to happen in a single transaction.
Is there any clean way of doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 EntityListener。这些必须放在您的所有实体上,所以如果您有很多实体,也许这不是一个好的解决方案。
否则,您可能需要一种特定于实现的方式:
Toplink: http://java-persistence.blogspot.com/2007/04/simple-auditing-using-toplink-jpa.html
休眠:http://community.jboss.org/wiki/AuditLogging
EclipseLink:http://www.eclipse .org/forums/index.php?t=tree&th=165280&#page_top
You could use EntityListeners. These would have to be put on all your Entities, so if you have a lot, maybe it's not a good solution.
Otherwise, you may need an implementation specific way:
Toplink: http://java-persistence.blogspot.com/2007/04/simple-auditing-using-toplink-jpa.html
Hibernate: http://community.jboss.org/wiki/AuditLogging
EclipseLink: http://www.eclipse.org/forums/index.php?t=tree&th=165280&#page_top