java - 可以同时使用 Hibernate 和 Tomcat 连接池吗?

标签 java hibernate jdbc

我正在开发一个 java web 应用程序并且我使用 Tomcat 连接池,这是我的设置:

<?xml version="1.0" encoding="UTF-8"?>
<Context path="" docBase="" debug="5" reloadable="true" crossContext="true">
<Resource name="jdbc/jdbcPool"
            auth="Container"
            type="javax.sql.DataSource"
            maxActive="100"
            maxIdle="30"
            maxWait="10000"
            username="root"
            password="*******"
            driverClassName="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost:3306/dbname?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>            
</Context>

和我的 DAO:

 public static Connection dbConnection() throws NamingException {
        Context initContext;
        DataSource ds = null;
        Connection conn = null;
        try {
            initContext = new InitialContext();
            Context envContext = (Context) initContext.lookup("java:/comp/env");
            ds = (DataSource) envContext.lookup("jdbc/jdbcPool");
            conn = ds.getConnection();        
        }catch (SQLException ex){
            logger.error("SQLException Occurred in DAO.dbConnection() Method, Exception Message is: " + ex.getMessage(), ex);
        }
        catch (RuntimeException er){
            logger.fatal("SQLException Occurred in DAO.dbConnection() Method, Exception Message is: " + er.getMessage(), er);
        }catch(Exception rt){
           logger.fatal("Exception Occurred in DAO.dbConnection() Method, Exception Message is: " + er.getMessage(), er);
        }
        return conn;
    }

我想使用 hibernate ,所以我重构了部分代码,现在我想知道我们是否可以在我的应用程序中同时使用它们(我的意思是我的部分代码使用 hibernate ,部分使用我的DAO 连接?) 如果是,那些没有映射到hibernate但一些映射表与它们有关系的表会发生什么?

最佳答案

我个人对 hibernate 的偏好是根本不为它配置连接池。这可以通过简单地在我们的 hibernate 配置中省略连接池设置并使用 openSession(Connection) 方法来完成:

Connection conn = ... // get from jndi
Session session = sessionFactory.openSession(connection);
try{
   //do some work with either hte connection or the session or both
}finally{
   session.close();
   conn.close();
}

这样做的好处是,您可以控制正在使用的连接及其分配位置,最重要的是它在何处关闭,如果您正在使用 hibernate 和 jdbc 代码执行事务,这可能很重要。

编辑:@ChssPly76 关于排除 hibernates 内置事务管理的观点,他说得很对,hibernate 提供合理的事务支持,如果给定的 JTA 将与任何正在进行的事务同步。在非 JTA 应用程序中,您需要 hibernate 和 jdbc 代码在同一个 jdbc 事务中运行,确保 hibernate Session 使用与 jdbc 代码相同的连接很重要,最好的方法是提供连接到 session 工厂。请注意,这并不排除使用 Hibernate 事务对象:

Connection conn = ... // get from jndi
Session session = sessionFactory.openSession(connection);
try{
   Transaction tx = new Transaction(); // 
   //do some work with either hte connection or the session or both
   tx.commit();
}finally{
   session.close();
   conn.close();
}

它会工作得很好。

关于java - 可以同时使用 Hibernate 和 Tomcat 连接池吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1163157/

相关文章:

java okhttp使用for循环动态添加 header

hibernate - 使用@ManyToMany 时出现 StackOverflowError

java - SQL错误: 17068 while inserting records in hibernate

hadoop - Cognos 报告 Hive 数据源很慢?

java - 如何在不设置区域设置的情况下设置 NLS 日期格式

java - 在java中写入文件 - 关闭或刷新

java - 来自一个 InputStream 的 2 个 BufferedReader

java.nio.charset.MalformedInputException : Input length = 1

java - 如何启用 Hibernate 自动 ddl 表创建?

java - 使用 Java 从 glassfish 服务器获取 jdbc 资源