-->
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: Manage Multiple Schemas with a c3p0 ConnectionCustomizer
PostPosted: Sun Mar 09, 2008 5:34 pm 
Newbie

Joined: Fri Apr 13, 2007 12:25 pm
Posts: 17
I have been trying to figure out the best way to manage the databases for a multi-organization software service, in which each organization has its own database. I have come up with the following solution, an implementation of the c3p0 ConnectionCustomizer:

Code:
package test.util.db;

import java.sql.Connection;
import java.sql.PreparedStatement;

import test.util.ThreadLocalDS;

import com.mchange.v2.c3p0.ConnectionCustomizer;

public class MyConnectionCustomizer implements ConnectionCustomizer {

   public void onAcquire(Connection arg0, String arg1) throws Exception {}
   public void onCheckIn(Connection arg0, String arg1) throws Exception {
      String sql = "use idle"; // move this back to a generic DB
      PreparedStatement prep = arg0.prepareStatement(sql);
      prep.execute();
   }

   public void onCheckOut(Connection arg0, String arg1) throws Exception {
      String sql = "use " + ThreadLocalDS.get();
      PreparedStatement prep = arg0.prepareStatement(sql);
      prep.execute();
   }

   public void onDestroy(Connection arg0, String arg1) throws Exception {}
}



Then the custom implementation is specified as a hibernate/c3p0 property in my hibernate.cfg.xml file:

Code:
   <property name="hibernate.c3p0.connectionCustomizerClassName">test.util.db.MyConnectionCustomizer</property>


The ThreadLocalDS is just a thread local variable set to the database schema name, which is set when the application requests a singleton instance of the Hibernate SessionFactory object.

I have seen many people on this board and elsewhere looking for a solution to this type of problem, and this has worked pretty well for me recently. I am looking for any comments on how well this will hold up in a high load/concurrency situation. Obviously if transactions with multiple schemas are interleaved this could cause issues if the thread local variable isn't re-set properly. But in the case where each conversation is with a single database (for example a servlet thread uses the specified schema related to the requesting user), this should work, right? Is there any case where a connection to the wrong database could get passed back to the thread?

Obviously this is a fairly insecure solution because the database user used has to have access to all the databases in the pool.


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.