views:

1402

answers:

2

Hi every one, I have a java web Application and I use Tomcat connection pooling for it, this 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;
    }

after that I want to use hibernate And I Refactor some part of my code but I want know can i 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 happen to that table not map in hibernate but some mapped table have relation to them? I don't know can explain my question right? if want more detail tell me.

+2  A: 

I suppose you can use them together, but why would you? You can configure Hibernate to use your data source instead like described in the manual. It would be something like:

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

As far as mappings go, "mapped" tables can have relations to "unmapped" tables (in the database), but those relations will also be "unmapped" (e.g. Hibernate won't be aware of them). So if you go that way you have to make sure you won't cause any referential integrity issues while trying to, say, insert / update "mapped" entity.

ChssPly76
Tanx for you answer, i must do some test on it to understand what exactly happen.
Am1rr3zA
+1  A: 

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.

Gareth Davis
you mean I omit my JNDI configuration in context.xml and when I want Quering DB (don't want use hibernate )get a connection from hibernate sessionFactory? then act like a common JDBC connection?
Am1rr3zA
@gid Can you elaborate why? Hibernate provides its own transation management (or you'll be using JTA) so why would you care when connection is closed - or, rather, returned to the pool? Seems like you're just manually doing a lot of what Hibernate would do for you. Am I missing something?
ChssPly76
I mean omit the connection settings (ie the JNDI reference and or any other references to connections from the hibernate configuration)
Gareth Davis