【问题标题】:Is there a way to set an existing connection into an entityManager有没有办法将现有连接设置到 entityManager
【发布时间】:2013-06-13 18:37:30
【问题描述】:

我有一个为我提供连接的连接处理程序: 代码在这里

handle = (JDBCHandler) m_broker.getBroker().getConnection();
Connection conn = handle.getConnection();

我想抓住这个连接并在 entityManager 中使用它并继续处理 entityManger 函数。

请问有办法吗?

【问题讨论】:

    标签: hibernate jpa-2.0 entitymanager


    【解决方案1】:

    好吧,我认为尝试这个并不常见,但是..

    由于您使用的是 Hibernate,您可以尝试使用来自 Hibernate Sessionreconnect 方法。

    首先你需要解开你的EntityManager(获取Hibernate的底层实现)然后使用reconnect方法,它接受一个Connection对象:

    entityManager.unwrap(Session.class).reconnect(connection);
    

    【讨论】:

    • 我正在尝试此操作,但出现错误:除非最初提供了 Connection,否则无法手动重新连接。有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-10
    • 1970-01-01
    • 2021-03-17
    • 2022-06-23
    • 2012-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多