Use tomcat's pooling mecanism :
in $TOMCAT/conf/server.xml, in your application Context, declare a Resource :
<Context path="/yourapp" docBase="yourapp">
<Manager className="org.apache.catalina.session.StandardManager"
pathname="">
</Manager>
<Resource name="jdbc/YOUR_DATABASE" scope="Shareable"
type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/YOUR_DATABASE">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>100</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>30000</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>100</value>
</parameter>
<parameter>
<name>username</name>
<value>sa</value>
</parameter>
<parameter>
<name>password</name>
<value>sa</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.microsoft.jdbc.sqlserver.SQLServerDriver(your
driver)</value>
</parameter>
<parameter>
<name>url</name>
<value>your.url.with.correct.driver</value>
</parameter>
<parameter>
<name>validationQuery</name>
<value>select getdate()</value>
</parameter>
</ResourceParams>
</Context>
You can, in your servlet, access the datasource :
try {
Context ctx = new InitialContext();
this.dataSource = (DataSource) ctx.lookup("java:comp/env/jdbc/YOUR_DATABASE");
} catch (NamingException e) {
log.error("Constructor throws NamingException " + e.getMessage());
}
And then get a connection from it :
Connection conn = dataSource.getConnection();
-----Message d'origine-----
De : frederik gens [mailto:[EMAIL PROTECTED]
Envoy� : vendredi 18 juin 2004 11:20
� : [EMAIL PROTECTED]
Objet : NEWBIE: flexible declaration of a database URL
Hello,
I'm currently developing a JSP/Servlet/JDBC application with Tomcat. In several
classes called by servlets, I declare the URL which identify the database in order to
open the connection.
My question is: what is the best way to declare this URL only once, for example in a
file, or in an environment variable. Therefore, the classes always call this variable.
Changing the place of the database just requires to change the URL in one place.
Thanks in advance!
Frederik
---------------------------------
Do you Yahoo!?
Yahoo! Mail is new and improved - Check it out!
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]