代码之家  ›  专栏  ›  技术社区  ›  Am1rr3zA

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

  •  5
  • Am1rr3zA  · 技术社区  · 16 年前

    我正在开发一个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>
    

     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连接?)

    2 回复  |  直到 9 年前
        1
  •  3
  •   ChssPly76    16 年前

    我想你们可以一起使用,但为什么呢?您可以将Hibernate配置为使用数据源,如中所述 manual . 可能是这样的:

    hibernate.connection.datasource = java:/comp/env/jdbc/jdbcPool
    hibernate.dialect = org.hibernate.dialect.MySQLDialect
    

        2
  •  3
  •   Gareth Davis    16 年前

    我个人对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代码执行事务,这一点可能很重要。

    编辑:关于@chsspy76关于排除hibernates内置事务管理的观点,他说得很对,hibernate提供了合理的事务支持,如果给定的JTA将与任何正在进行的事务同步。在一个非JTA应用程序中,您需要hibernate和jdbc代码在同一个jdbc事务中运行,确保hibernate会话使用与jdbc代码相同的连接非常重要,最好的方法是将连接提供给会话工厂。注意,这并不排除使用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();
    }
    

    它会很好用的。

    推荐文章