Re: Connection Pooling



Chase Preuninger wrote:
What is the best way to create/get a database pool for a serious web
application?

Because of WEB Application a simple sample for Tomcat.

1.Declare Datasource in your Context.xml, e.g.:
<Resource name="MyDatabase"
auth="Container" type="javax.sql.DataSource"
maxActive="10" maxIdle="30" maxWait="10000"
username="sa"
password=""
driverClassName="org.h2.Driver"
url="jdbc:h2:tcp://localhost:9092/hibernate" />

2.Add a resource-ref to your web.xml:
<resource-ref>
<res-ref-name>MyDatabase</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

Tomcat build connection pool by themself. Look for
file tomcat-dbcp.jar in Tomcats lib-directory.

3.Get the connection:
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup(
"java:comp/env/MyDatabase");
Connection con = ds.getConnection();

4.Close the connection _every_time_ in your SQL source
within _finally_block_. It does not close the connection
but put it back into the pool.

Alfred


.



Relevant Pages

  • Re: DriverManager vs. DataSource?
    ... > class to establish the connection. ... it is possible to use the DriverManager interface. ... JDBC driver jars go in a 'common' directory in the container. ... > the DataSource interface will typically be registered with a JNDI ...
    (comp.lang.java.databases)
  • Re: Connection for Data Source in SSAS 2005 Tutorial Lesson 6
    ... double click on the datasource and then click on ... I noticed that my server explorer showed a connection to ... When connecting to SQL Server 2005, this failure may be caused by the ... not be made to the data source with the DataSourceID of 'Adventure ...
    (microsoft.public.sqlserver.olap)
  • Re: ConnectionPoolDataSource + MM MySQL + Tomcat4
    ... to work with Tomcat 5.0.28 and MySQL or Oracle. ... <ResourceParams> ... in functions needing DB access, use the DataSource variable ... to create a Connection ...
    (comp.lang.java.programmer)
  • Re: mail merge code template with connection strings
    ... 'Get the details of the datasource ... .FirstRecord = wdDefaultFirstRecord ... I am currently connecting with an ODBC connection, ... Set rst = New ADODB.Recordset ...
    (microsoft.public.word.mailmerge.fields)
  • Re: Read from one, write into another data source
    ... this sounds like you're using ADO to to transport (bulk copy) data. ... > contents into another DataSource. ... > OleDbConnection connection = adapter.SelectCommand.Connection; ...
    (microsoft.public.data.ado)