从Stateless Bean获取一个JDBC Connection对象 [英] Get hold of a JDBC Connection object from a Stateless Bean

查看:137
本文介绍了从Stateless Bean获取一个JDBC Connection对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在无状态会话Bean中,注入了一个 EntityManager ,但是我想要保留一个 Connection 对象命令调用DB过程。
有没有解决方法?

In a Stateless Session Bean an EntityManager is injected but I would like to get hold of a Connection object in order to invoke a DB Procedure. Is there any solution to this ?

推荐答案

这将是JPA提供者特定的代码。通常情况下,这可以通过调用 unwrap() EntityManager 类。

This is going to be JPA provider specific code. Typically this is done by invoking unwrap() on the EntityManager class.

如果您正在使用EclipseLink,以下代码(来自 EclipseLink wiki )将是有用的(在案例您正在使用应用程序管理的EntityManager):

If you are using EclipseLink, the following code (from the EclipseLink wiki) will be useful (in the case you are using an application-managed EntityManager) :

JPA 2.0

entityManager.getTransaction().begin();
java.sql.Connection connection = entityManager.unwrap(java.sql.Connection.class); // unwraps the Connection class.
...
entityManager.getTransaction().commit();

JPA 1.0

entityManager.getTransaction().begin();
UnitOfWork unitOfWork = (UnitOfWork)((JpaEntityManager)entityManager.getDelegate()).getActiveSession();
unitOfWork.beginEarlyTransaction();
Accessor accessor = unitOfWork.getAccessor();
accessor.incrementCallCount(unitOfWork.getParent());
accessor.decrementCallCount();
java.sql.Connection connection = accessor.getConnection();
...
entityManager.getTransaction().commit();

请注意,为JPA 2.0提供的解决方案将失败,Hibernate 3.6.5具有 PersistenceException 包含消息

Note, that the solution provided for JPA 2.0 will fail for Hibernate 3.6.5 with a PersistenceException containing the message


Hibernate无法解压缩接口java.sql.Connection

Hibernate cannot unwrap interface java.sql.Connection

使用Skaffman提供的代码使其适用于Hibernate(即使对于容器管理的持久性上下文也可以在3.6.5以下工作)。

Use the code provided by Skaffman to get it to work against Hibernate (verified to work under 3.6.5 even for container managed persistence contexts).

但是,EclipseLink wiki指出一个有用的信息 - 如果您使用JTA管理的数据源,则应该使用 @Resource 注释或使用JNDI查找检索它。只要您需要对数据库执行事务处理工作,是否从数据源或现有数据源获取新的连接是无关紧要的。大多数连接池将无论如何提供与当前线程(即实体管理器已经使用的线程)相关联的相同连接。因此,您将避免以这种方式展开实体管理器,并且还对数据库执行事务性活动;请记住,如果执行此操作,持久化上下文缓存和二级缓存可能不会同步。

However, the EclipseLink wiki points out one useful bit of info - if you are using JTA managed datasources, you should be injecting it using the @Resource annotation or retrieving it using a JNDI lookup. As long as you need to perform transactional work against the database, it is immaterial as to whether you are obtaining a new connection from the data source or an existing one; most connection pools will anyway provide the same connection that is associated with the current thread (i.e. the one already used by the entity manager). You would therefore avoiding unwrapping the entity manager this way, and also perform transactional activity against the database; do remember that the persistence context cache, and a second-level cache may not be synchronized if you do this.

这篇关于从Stateless Bean获取一个JDBC Connection对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆