可以同时使用Hibernate和Tomcat连接池吗? [英] can Use Hibernate and Tomcat Connection pool at same time?

查看:167
本文介绍了可以同时使用Hibernate和Tomcat连接池吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b

 <?xml我开发了一个java web应用程序,并使用Tomcat连接池,这里是我的设置: version =1.0encoding =UTF-8?> 
< Context path =docBase =debug =5oadable =truecrossContext =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; amp; amp; amp; useUnicode = true& amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp;
< / Context>

和我的DAO:

  public static Connection dbConnection()throws NamingException {
Context initContext;
DataSource ds = null;
连接conn = null;
尝试{
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(在DAO.dbConnection()方法中发生SQLException,异常消息是:+ ex.getMessage(),ex);
}
catch(RuntimeException er){
logger.fatal(DAO.dbConnection()方法中发生SQLException,异常消息是:+ er.getMessage(),er);
} catch(Exception rt){
logger.fatal(DAO.dbConnection()方法中发生异常,异常消息是:+ er.getMessage(),er);
}
返回conn;





$ b我想使用hibernate,所以我重构了一部分代码,现在我想知道对我来说是否可能我们在我的应用程序中使用它们(我的意思是我的代码的一部分使用hibernate并且一些部分使用我的DAO连接?)
如果是的话,那些表格会发生什么映射与休眠,但一些映射表与他们有关系?

解决方案

我个人喜欢与休眠是不配置它与一个连接池。这可以通过简单地省略我们的hibernate配置中的连接池设置并使用openSession(Connection)方法来完成:

 连接conn = ... //从jndi获得
Session session = sessionFactory.openSession(connection);
尝试{
//使用连接或会话或
}做一些工作,最后{
session.close();
conn.close();
}

这样做的好处是您可以控制正在使用的连接,它在哪里被分配,最重要的是它被关闭的地方,如果你使用hibernate和jdbc代码来执行一个事务,这可能很重要。



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

 连接conn = ... //从jndi获取
Session session = sessionFactory.openSession(connection);
尝试{
Transaction tx = new Transaction(); //
//使用连接或会话或者同时使用
tx.commit();
} finally {
session.close();
conn.close();
}

它可以正常工作。


I am developing a java web Application and I use Tomcat connection pooling, here is my setting:

<?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>

and my 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;
    }

I want to use hibernate so I refactor some part of my code, now I want to know is it possible for me us use both of them in my application (I mean some part of my code use hibernate and some part use my DAO connection?) If yes, what's gonna happen to those tables that not mapped with hibernate but some mapped tables have relation with them?

解决方案

My personal preference with hibernate is is not to configure it with a connection pool at all. This can be done by simply omitting the connection pool settings in our hibernate configuration and using the openSession(Connection) method:

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

This has the advantage that you are in control of which connection is being used and where it is allocated, and most importantly where it is closed, this may be important if you are performing a transaction using hibernate and jdbc code.

EDIT: on @ChssPly76 point about excluding hibernates inbuilt transaction management, and he is quite right, hibernate provides reasonable transaction support and if a given a JTA will synchronise with any on going transaction. In a none JTA app where you require both hibernate and jdbc code to operate in the same jdbc transaction it is important to make sure that the hibernate Session is using the same Connection as the jdbc code, the best way to do this is to give the Connection to the session factory. Note this doesn't exclude using a Hibernate transaction object:

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

it'll work just fine.

这篇关于可以同时使用Hibernate和Tomcat连接池吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆