在我的DAO
课程中,我引用了一个EntityManager
. 我想EntityManager
通过使用来访问线程安全ThreadLocal
。
到目前为止,我的尝试只产生了NullPointerException
s,而且我似乎找不到一个像样的例子。
有人可以为我提供一个例子或指出我正确的方向吗?
更新:我已经尝试了 BalusC 的建议,但是当我同时通过 JSF 和 JAX-RS 网络服务访问 DAO 时,我仍然遇到错误:
org.hibernate.exception.GenericJDBCException: could not load an entity
java.sql.SQLException: You can't operate on a closed Connection!!!
java.lang.NullPointerException
at com.mchange.v2.c3p0.impl.NewProxyConnection.prepareStatement
我正在使用 C3P0,所以我不知道为什么关闭连接是一个问题。
update2:BalusC 的最后一条评论似乎解决了我的问题:At least, you should not have a single instance of the DAO class shared throughout the application. Create a new one on every request.