java - Hibernate返回 "weird"连接

标签 java mysql hibernate jpa

我在一个带有 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;
    }
}

我使用这种方法将记录插入到我的数据库中:

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();
            }
        }

    }

实现如下:

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

没什么特别的。除了..它不起作用。没有异常(exception),什么都没有。但记录未插入。选择按预期进行。它返回记录。然后我交换了连接。我以经典方式创建了连接:

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

传递了c而不是ConnectionUtil.getConnection()并且它起作用了。

但是,这种情况下的逻辑问题是:

如果我无法使用 getConnection 方法返回的连接插入记录,那么 Hibernate 使用相同的连接如何插入记录?

最佳答案

ConnectionProvider 不适合由应用程序使用,请检查 documentation

The ConnectionProvider interface is not intended to be exposed to the application. Instead it is used internally by Hibernate to obtain connections.

因此,更好的选择是第二种方法,以经典方式建立连接。

关于java - Hibernate返回 "weird"连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28588169/

相关文章:

Java Scanner 用于在不前进位置的情况下窥视某些东西(int、double、line 等)

mysql - 使用连接和组对 MySQL 查询进行排序

php - 策划新闻订购系统

mysql - 截断了不正确的 DOUBLE 值 : '-'

java - EnversSchemaGenerator 无法解析为类型

java - 无法删除或更新双向关系中的父行

java - 对带有删除标志的数据使用 HQL 的正确方法是什么

java - 使用字节数组作为 Map 键

java - FileInputStream/FileOutputStream 阻塞?

Java 和 HttpUnit 存在 GZip 错误