Can I use Hibernate and Tomcat Connection at the same time?

I am developing a Java application and using the Tomcat connection pool, here is my setup:

<?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 will reorganize part of my code, now I want to know if it is possible for us to use both of them in my application (I mean that part of my code uses sleep mode, and part uses my DAO connection ?) If so, what happens to those tables that are not mapped to hibernation, and some mapped tables are related to them?

+5
source share
2 answers

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

EDIT: @ChssPly76 , , hibernate , JTA . JTA, hibernate, jdbc- jdbc, , hibernate , jdbc, - factory. , 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();
}

.

+2

, , ? Hibernate , manual. - :

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

, "" "" ( ), " " (, Hibernate ). , , , - , , , / "" .

+3

All Articles