-->
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.  [ 1 post ] 
Author Message
 Post subject: Using OracleOCIConnectionPool with Hibernate
PostPosted: Thu Feb 12, 2009 10:52 am 
Newbie

Joined: Mon Jan 01, 2007 5:45 am
Posts: 4
Hello all

Just wanted to share with you my experience with using the OracleOCIConnectionPool class as hibernate's connection pooling mechanism.

After trying both proxool and C3P0, I decided to try Oracle's native pool implementation, since I use Oracle + Oracle client with my system.

If you want to use OCI as your Hibernate's connection pooling, you first need to implement the ConnetionProvider interface which acts as a bridge between Hibernate and the OracleOCIConnectionPool class.

Here is my implementation:
Code:
public class OCIConnectionProvider implements ConnectionProvider {

   private OracleOCIConnectionPool pool;
   
   public OCIConnectionProvider() {
   }
   
   @Override
   public void configure(Properties props) throws HibernateException {
      
      try {
         // Mandatory properties
         String url = props.getProperty("connection.url");
         String userName = props.getProperty("connection.username");
         String password = props.getProperty("connection.password");
      
         pool = new OracleOCIConnectionPool();
         pool.setURL(url);
         pool.setUser(userName);
         pool.setPassword(password);
         
         
         // Pool properties
         Properties poolConfig = new Properties();
         for (Entry<Object,Object> entry : props.entrySet()) {
            if (entry.getKey().equals("oci.min_limit")) {
               poolConfig.put(OracleOCIConnectionPool.CONNPOOL_MIN_LIMIT, entry.getValue());
            } else if (entry.getKey().equals("oci.max_limit")) {
               poolConfig.put(OracleOCIConnectionPool.CONNPOOL_MAX_LIMIT, entry.getValue());
            } else if (entry.getKey().equals("oci.increment")) {
               poolConfig.put(OracleOCIConnectionPool.CONNPOOL_INCREMENT, entry.getValue());
            } else if (entry.getKey().equals("oci.timeout")) {
               poolConfig.put(OracleOCIConnectionPool.CONNPOOL_TIMEOUT, entry.getValue());
            } else if (entry.getKey().equals("oci.nowait")) {
               poolConfig.put(OracleOCIConnectionPool.CONNPOOL_NOWAIT, entry.getValue());
            }
         }
         pool.setPoolConfig(poolConfig);
         
      } catch (Exception e) {
         throw new HibernateException("Cannot configure connection pool",e);
      }
   }

   @Override
   public Connection getConnection() throws SQLException {
      return pool.getConnection();
   }
   
   @Override
   public void closeConnection(Connection conn) throws SQLException {
      conn.close();
   }

   @Override
   public void close() throws HibernateException {
      try {
         pool.close();
      } catch (SQLException e) {
         throw new HibernateException(e);
      }
   }

   @Override
   public boolean supportsAggressiveRelease() {
      return false;
   }

}


As you can see, the implementation is pretty straight forward, the main thing here is to deal with properties mapping from Hibernate's config file to the properties supported by the OCI pool.

The next thing is to change the Hibernate config file to something like this:
Code:
<hibernate-configuration>

    <session-factory>

        <property name="default_schema">MY_SCHEMA</property>
      <property name="connection.provider_class">mypackage.OCIConnectionProvider</property>
       
        <property name="connection.url">jdbc:oracle:oci:@myoracle</property>
        <property name="connection.username">myuser</property>
        <property name="connection.password">mypass</property>
       
      <property name="oci.min_limit">1</property>
      <property name="oci.max_limit">10</property>
      <property name="oci.increment">1</property>
      <property name="oci.timeout">10</property>
   
     ....  more configurations ....
     </session-factory>
</hibernate-configuration>


Note that I pass the OCI pool parameters using the session-factory parameters mechanism.

That's it basically. To run this, you need to have OCI on your machine. I am using OCI Instant Client which works great and is small
(see: http://www.oracle.com/technology/tech/o ... index.html)

Hope this helps, I would love the get comments / tips regarding this issue .

Ran.


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

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.