-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: DBCP and hibernate.properties
PostPosted: Mon Jan 26, 2004 4:27 pm 
Newbie

Joined: Mon Jan 26, 2004 4:21 pm
Posts: 10
Hi all,

I'm trying to use DBCP with Hibernate, but it seems I can't bypass Hibernate's DriverManagerConnectionProvider.

I would like to avoid using the Hibernate properties file to configure the DB's url, password etc. To that end, I store the connection pool in JNDI (I use tomcat's JNDI implementation), where I define all its properties.

My problem is that when I build a session factory, the code ends up using the hibernate.connection properties from the hibernate.properties file I keep around for development to create a DriverManagerConnectionProvider. This file defines a JDBC driver that's not available in the production environemnt, so I get an exception.

I was hoping that this would work:

Code:
// Called once in the webapp's lifetime
init() {
// builds the configuration
cfg = new Configuration();
cfg.addClass(...);
cfg.addClass(...);

// Get the datasource from jndi
Context ctx = null;
ctx = new InitialContext();
m_datasource = (DataSource)ctx.lookup( "java:comp/env/jdbc/connectionPool");

// Build the session factory
m_sessionFactory = cfg.buildSessionFactory();


Session getSession(){
Connection conn = m_datasource.getConnection();
return  m_sessionFactory.openSession(conn);
}


Is there a way I can make this work?

I guess I don't understand why cfg.buildSessionFactory() looks up the hibernate.connection.driver_class property


The reason why I don't want to configure dbcp in hibernates.properties (except during development) is that I want the application to be entirely congigured via JNDI.


Thanks for you help,

Susie.

Code:


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2004 6:26 pm 
Newbie

Joined: Mon Jan 26, 2004 4:21 pm
Posts: 10
Hmmm, loks like I can make it work by defining this property in hibernate.properties:

Code:
hibernate.connection.datasource=java:comp/env/jdbc/connectionPool



Now my question is: do I need to obtain a connection from the pool and pass it to to get session:

Code:
Session getSession(){
Connection conn = m_datasource.getConnection();
return  m_sessionFactory.openSession(conn);
}


or do I just need to call the no-argument method?

Code:
Session getSession(){
return  m_sessionFactory.openSession(); // will this retrieve a connection from dbcp?
}


Thanks and sorry for these newbie's questions,

Susie.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2004 6:30 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Just call the no-argument method.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2004 7:14 pm 
Regular
Regular

Joined: Fri Sep 05, 2003 12:01 am
Posts: 80
Location: Bogot
why not use hibernate.cfg.xml ? Just put it in the root of your classpath...

Here is an example with DBCP :

Code:
<?xml version='1.0' encoding='utf-8'?>

<!DOCTYPE hibernate-configuration PUBLIC

        "-//Hibernate/Hibernate Configuration DTD 2.0//EN"



"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">



<hibernate-configuration>

    <!-- a SessionFactory instance listed as /jndi/name -->

    <session-factory         >




  <property name="hibernate.dialect">net.sf.hibernate.dialect.SybaseDialect</property>
  <property name="show_sql">false</property>
  <property name="use_outer_join">true</property>


<!-- In case you have a JNDI Datasource   
<property name="hibernate.connection.datasource">java:/PAPAONEDS</property>
-->

        <!-- DBCP  properties   --> 
        <property name="hibernate.connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
        <property name="hibernate.connection.password">XXX</property>
        <property name="hibernate.connection.username">yyyy</property>

        <property name="hibernate.connection.url">jdbc:microsoft:sqlserver:/HOST:1433;database=DB;selectMethod=cursor</property>
        <property name="hibernate.connection.pool_size">10</property>
        <property name="hibernate.connection.pool_alias">pool1</property> 
         

 
  <property name="hibernate.jdbc.batch_size">0</property>
  <property name="hibernate.jdbc.use_scrollable_resultsets">false</property>


        <!-- mapping files go here... -->

_________________
Mauricio Hern


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.