简体   繁体   English

Hibernate返回“奇怪”连接

[英]Hibernate returns “weird” connection

I'm using JPA in one project with Hibernate and I had a need to perform some operations on pure JDBC level so I used this method to obtain connection to my database: 我在与Hibernate一起的一个项目中使用JPA,并且我需要在纯JDBC级别上执行一些操作,因此我使用此方法来获取与数据库的连接:

public class ConnectionUtil {
    public static EntityManagerFactory EMF = Persistence
        .createEntityManagerFactory("persistenceUnitName");

    public static Connection getConnection() throws SQLException {

        Connection connection = null;
        EntityManager em = null;
        try {
            em = EMF.createEntityManager();
            Session session = (Session)EMF.createEntityManager()
                    .getDelegate();
            SessionFactoryImplementor sfi = (SessionFactoryImplementor) session
                    .getSessionFactory();
            @SuppressWarnings("deprecation")
            ConnectionProvider cp = sfi.getConnectionProvider();
            connection = cp.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (em != null) {
                em.close();
            }
        }

        return connection;
    }
}

And I used this method for insertion of records into my database: 我使用这种方法将记录插入到数据库中:

public void saveOrUpdate(String sqlStatement, Connection connection,  Object... args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;

        try {

            preparedStatement = connection.prepareStatement(sqlStatement);
            for (int i = 0; i < args.length; i++) {
                preparedStatement.setObject(i + 1, args[i]);
            }
            preparedStatement.execute();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

Implementation looks like this: 实现看起来像这样:

saveOrUpdate("INSERT INTO my_table(my_column) VALUES (?)", ConnectionUtil.getConnection(), "MyValue");

Nothing special. 没什么特别的。 Except.. it doesn't work. 除了..它不起作用。 No exceptions, nothing. 没有例外,没有。 But record is not inserted. 但是未插入记录。 Selection works as intended. 选择按预期工作。 It returns records. 它返回记录。 Then I swapped connections. 然后我交换了连接。 I created connection in classic way: 我以经典方式创建了连接:

  Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/db","u","p");

passed c instead of ConnectionUtil.getConnection() and it worked. 传递了c而不是ConnectionUtil.getConnection()并成功了。

However, the logical question that follows this situation is: 但是,这种情况之后的逻辑问题是:

If I cannot insert record by using connection returned by getConnection method, then how can Hibernate insert a record if it's using that same connection? 如果我无法使用getConnection方法返回的连接来插入记录,那么如果Hibernate使用相同的连接,那么如何插入记录呢?

The ConnectionProvider is not intended to be used by the application, check the documentation 该应用程序不打算使用ConnectionProvider ,请查阅文档

The ConnectionProvider interface is not intended to be exposed to the application. ConnectionProvider接口不打算向应用程序公开。 Instead it is used internally by Hibernate to obtain connections. 而是由Hibernate在内部使用它来获取连接。

So, the better option is your second approach, to get the connection in a classic way. 因此,更好的选择是您的第二种方法,以经典方式获得连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM