我们正在将基于 JDBC 的应用程序迁移到 JPA 和 EJB3。我们的旧应用程序使用 Connect#setClientInfo API 来记录当前用户名作为客户端信息的一部分:
http://download.oracle.com/javase/6/docs/api/java/sql/Connection.html#setClientInfo%28java.lang.String,%20java.lang.String%29
我们需要在 EJB3 项目中做类似的事情。如何?
我们可以在 EJB 服务调用周围使用 EJB3 拦截器来捕获当前用户并将其设置为数据源上的信息。然而,我看到了这方面的问题。我认为当 JPAlush() 发生时没有保证。如果您在拦截器中设置客户端信息,进行一些更新,然后返回,则在您的 bean(和拦截器)超出范围之后,flush() 和实际的数据库写入可能不会发生。这是正确的吗?
我相信 JPA 和 EntityManager 是连接上的抽象,您无法可靠地设置连接上的客户端信息。是真是假?
We are migrating a JDBC based application to JPA and EJB3. Our old application used the Connect#setClientInfo API to record the current user name as part of the client info:
http://download.oracle.com/javase/6/docs/api/java/sql/Connection.html#setClientInfo%28java.lang.String,%20java.lang.String%29
We need to do something similar in the EJB3 project. How?
We can use EJB3 interceptors around the EJB service calls in order to capture the current user and set it as info on the datasource. However, I see problems with this. I think there is no guarantee when the JPA flush() occurs. If you set the client info in an interceptor, make some updates, and then return, the flush() and actual database write may not occur until well after your bean (and interceptor) are out of scope. Is this correct?
I believe JPA and EntityManagers are abstractions over the connection, and you cannot reliable set the client info on the connection. True or false?
发布评论
评论(1)
您使用哪个 JPA 提供商?
EclipseLink 支持基于用户的连接、Oracle 代理连接和 VPD。 EclipseLink 还定义了会话和连接级别事件,允许您在 JDBC 连接上设置配置。
看,
http://wiki.eclipse.org/EclipseLink/Examples/JPA/Auditing
What JPA provider are you using?
EclipseLink has support for user based connection, Oracle proxy connections and VPD. EclipseLink also defines Session and connection level events that allow you to set configuration on the JDBC connection.
See,
http://wiki.eclipse.org/EclipseLink/Examples/JPA/Auditing