db-torque-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Anuradha S.Athreya" <anuradh...@world2web.com>
Subject RE: Creating common connection pool
Date Mon, 05 Dec 2005 06:50:37 GMT
Hi Greg,

I did try this option and it worked like a charm!

Couple of clarifications:
1. This is the ConnectionPooling mechanism I'm using - as set in the
Torque.properties file:

	## Using commons-dbcp 
 
torque.dsfactory.bookstore.factory=org.apache.torque.dsfactory.SharedPoolDat
aSourceFactory
	
#torque.dsfactory.bookstore.factory=org.apache.torque.dsfactory.PerUserPoolD
ataSourceFactory
	torque.dsfactory.bookstore.pool.defaultMaxActive=10
	torque.dsfactory.bookstore.pool.testOnBorrow=true
	torque.dsfactory.bookstore.pool.validationQuery=SELECT 1
	torque.dsfactory.bookstore.connection.driver = com.mysql.jdbc.Driver
	torque.dsfactory.bookstore.connection.url =
jdbc:mysql://localhost:3306/anuradhasa
	torque.dsfactory.bookstore.connection.user = anuradhasa
	torque.dsfactory.bookstore.connection.password = anuradhasa


Now, in the code to get a connection, when I invoke: 
Connection conn = Torque.getConnection();  --- I observed that it is
creating a new connection object every time, instead of using the same
connection object from the connection pool. Is this how it should happen?

2. We are currently using Turbine 2.1 ONLY for connection pooling. Is it
possible for me to initialise turbine similar to how we initialised torque,
common to 2 servlet contexts? If I am able to achive this, I could continue
with the Turbine, instead of movinng to Torque.

Regards
Anuradha

-----Original Message-----
From: Greg Monroe [mailto:Greg.Monroe@DukeCE.com] 
Sent: Friday, December 02, 2005 8:43 PM
To: Apache Torque Users List; anuradhasa@world2web.com
Subject: RE: Creating common connection pool

Here's what I do:

1) Make sure your JDBC drivers are in the <CATALINA_HOME>/common/lib
directory.  This ensures that the exact same classes will be shared across
all web apps and let you use them with Server level resources like Tomcat's
SingleSignOn valve and my help with Global Resource definitions.

2) In each WebApp, use a common ContextListener implimentation class in the
WebApp's WEB.XML file to initialize things. Eg: add:

  <listener>
  	<listener-class>xxx.xxx.init.ContextListener</listener-class>
  </listener>

Torque is initialized in the
init.ContextListener.contextInitialized(...)
method.  Note that because Torque should be initialized just once and each
context using this will call this method, you should wrap your Torque init
code in a check to see if it's already been initialized.

Here's a simplified version of the code I use:

logger.info("Starting context: "+ arg0.getServletContext().
              getServletContextName());

if ( ! Torque.isInit() {
// Using getResource so real location of file can be found. 
    URL url = this.getClass().getClassLoader().
                getResource("Torque.properties"); // Note:  Don't use new
URI(url.toString()) - Tomcat will not resolve this!.
    String path = url.getPath();
    if ( path != null && ! path.startsWith("/")) {
        path = "/"+path;
    }
    URI uri = new URI(url.getProtocol(),"",path,null);
    logger.debug("getDBConfigurationFile: Torque config url ="+
             uri.toString());
    File configFile = new File(uri);	
    Torque.init(configFile.toString());
}

3) With the above code, the classpath (including jars) will be searched for
the "Torque.properties" file.  To have a single point of DB init, put a
Torque.properties file in the common/classes directory.

For extra credit, you could think about using a ServletContext init
parameter in the Web.xml to allow for alternate Torque properties file names
rather than just a hard coded default.

Oh, and this works fine with simple shared connection pooling.  FWIW, I
started using JNDI resources but found that when apps got turned over to
system admins to install on production servers, they tended to get one or
more of the XML edits required to install it wrong.. 
leading to more support time.  So, I dropped the "right" way to do things
for the less support way.  This way, once a server's common code is set up,
apps can be distributed by a simple war file.

 
> -----Original Message-----
> From: Anuradha S.Athreya [mailto:anuradhasa@world2web.com]
> Sent: Friday, December 02, 2005 5:21 AM
> To: torque-user@db.apache.org
> Subject: Creating common connection pool
> 
> 
> I am trying to configure connection pooling using Torque for
> 2 web applications deployed in the same tomcat container but in 2 
> different contexts,connecting to the same DB.
> 
> Doubt 1: Where should the Torque.properties file be located? 
> Obviously, the path of this file has to be with reference to a servlet 
> context. Since I want to create a common connection, which one of the 
> 2 servlet context will contain the properties file?
> 
> I made changes to the Tomcat server.xml file : 
> 
> <Context path="/myApp1" 
> docBase="D:/jakarta-tomcat-5.0.18/webapps/myApp1" >
> 				    <Resource name="jdbc/myApp1"
> scope="Shareable" type="javax.sql.DataSource"/>
> 				    <ResourceParams name="jdbc/myApp1">
> 				        <parameter>
> 				            <name>factory</name>
> 	
> <value>org.apache.torque.dsfactory.JndiDataSourceFactory</value>
> 				        </parameter>
> 				
> 				        <!-- DBCP database connection
settings -->
> 				        <parameter>
> 				            <name>url</name>
> 	
> <value>jdbc:mysql://localhost/myApp1DB</value>
> 				        </parameter>
> 				        <parameter>
> 	
> <name>driverClassName</name><value>com.mysql.jdbc.Driver</value>
> 				        </parameter>
> 				        <parameter>
> 				            <name>username</name>
> 				            <value>myApp1</value>
> 				        </parameter>
> 				        <parameter>
> 				            <name>password</name>
> 				            <value>myApp1</value>
> 				        </parameter>
> 				
> 				        <!-- DBCP connection
> pooling options
> -->
> 				        <parameter>
> 				            <name>maxWait</name>
> 				            <value>30000</value>
> 				        </parameter>
> 				        <parameter>
> 				            <name>maxIdle</name>
> 				            <value>100</value>
> 				        </parameter>
> 				        <parameter>
> 				            <name>maxActive</name>
> 				            <value>10</value>
> 				        </parameter>
> 				    </ResourceParams>
> </Context>
> 
> Under normal circumstances, since i place the war files of the 
> application, I dont specify the CONTEXT explicitly in the server.xml 
> file. But since. I need to place RESOURCE, here I added the CONTEXT 
> tag.
> Since, I have 2 web applications deployed, will I have to repeat the 
> CONTEXT tag again for my 'myApp2'??
> 
> I have also made changes to the Tomcat web.xml file..
> 
> Added:
> <resource-ref>
>       <description>DB Connection</description>
>       <res-ref-name>jdbc/instacoll</res-ref-name>
>       <res-type>javax.sql.DataSource</res-type>
>       <res-auth>Container</res-auth>
>   </resource-ref>
> 
> 
> Next comes the connection part....
> 
> Will Tomcat itself perform Torque.init()? 
> 
> If not, where should this be done, so that the Torque initialisation 
> happens common to both the applications??
> 
> 
> 

Duke CE Privacy Statement
Please be advised that this e-mail and any files transmitted with it are
confidential communication or may otherwise be privileged or confidential
and are intended solely for the individual or entity to whom they are
addressed.  If you are not the intended recipient you may not rely on the
contents of this email or any attachments, and we ask that you  please not
read, copy or retransmit this communication, but reply to the sender and
destroy the email, its contents, and all copies thereof immediately.  Any
unauthorized dissemination, distribution or copying of this communication is
strictly prohibited.



---------------------------------------------------------------------
To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
For additional commands, e-mail: torque-user-help@db.apache.org


Mime
View raw message