Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="b23257eaed112b6a-471c4171-4d854e2e-afae9d64-38723d979f1c9e90c539bebd"><ac:plain-text-body><![CDATA[

http://jakarta.apache.org/commons/dbcp/images/dbcp-logo-white.png

[http://jakarta.apache.org/commons/dbcp/ Commons-DBCP] provides database connection pooling services.[BR]A lot of information is available on the [http://jakarta.apache.org/commons/dbcp/ DBCP website]. If you don't find the information you need you can always contact us using one of the [http://jakarta.apache.org/site/mail2.html#Commons mailing lists].

]]></ac:plain-text-body></ac:structured-macro>

...

Diagrams hosted by http://rei1.m-plify.net

Tomcat 5.0 Configuration examples

Wiki Markup
Some Tomcat JNDI Datasource examples (in addition to the \[http://jakarta.apache.org/tomcat/tomcat-5.0-doc/jndi-datasource-examples-howto.html Tomcat 5.0 JNDI datasource howto\]).

BasicDataSource

No Format
<Resource name="jdbc/abandoned" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/abandoned">
   <parameter><name>factory</name><value>org.apache.commons.dbcp.BasicDataSourceFactory</value></parameter>
   <parameter><name>username</name><value>sa</value></parameter>
   <parameter><name>password</name><value></value></parameter>
   <parameter><name>driverClassName</name><value>org.hsqldb.jdbcDriver</value></parameter>
   <parameter><name>url</name><value>jdbc:hsqldb:database</value></parameter>
   <parameter><name>removeAbandoned</name><value>false</value></parameter>
   <parameter><name>removeAbandonedTimeout</name><value>300</value></parameter>
   <parameter><name>logAbandoned</name><value>true</value></parameter>
   <parameter><name>maxActive</name><value>10</value></parameter>
</ResourceParams>

...

No Format
<Resource name="jdbc/TestDBCPDS" auth="Container" type="org.apache.commons.dbcp.cpdsadapter.DriverAdapterCPDS"/>
<ResourceParams name="jdbc/TestDBCPDS">
   <parameter><name>factory</name><value>org.apache.commons.dbcp.cpdsadapter.DriverAdapterCPDS</value></parameter>
   <parameter><name>user</name><value>sa</value></parameter>
   <parameter><name>password</name><value></value></parameter>
   <parameter><name>driver</name><value>com.sybase.jdbc2.jdbc.SybDrivers</value></parameter>
   <parameter><name>url</name><value>jdbc:sybase:Tds:<myServerName>:<myPort>?charset=iso_1</value></parameter>
</ResourceParams>

<Resource auth="Container" name="jdbc/TestDB" type="org.apache.commons.dbcp.datasources.PerUserPoolDataSource"/>
<ResourceParams name="jdbc/TestDB">
   <parameter><name>factory</name><value>org.apache.commons.dbcp.datasources.PerUserPoolDataSourceFactory</value></parameter>
   <parameter><name>defaultMaxActive</name><value>10</value></parameter>
   <parameter><name>defaultMaxIdle</name><value>2</value></parameter>
   <parameter><name>defaultMaxWait</name><value>-1</value></parameter>
   <parameter><name>dataSourceName</name><value>java:comp/env/jdbc/TestDBCPDS</value></parameter>
</ResourceParams>

Tomcat 5.5 Configuration examples

Less DBCP, more Tomcat (and Tomcat 5.5.7 in particular):

A more specialized example, in which we want to set up a Tomcat Authentication Realm based on a database. The database shall be accessed through connections from a DBCP pool. We edit server.xml directly. This configuration can be tricky to get right, so here is a complete example:

First, define the 'javax.sql.DataSource' available to web applications in the JNDI default context under 'jdbc/m3p_5_0' by:

1: Saying what class or interface a JNDI context lookup will return: 'javax.sql.DataSource'.

2: Saying what class will actually create instances of the above, i.e. give the factory. We use the 'org.apache.commons.dbcp.BasicDataSourceFactory'. It creates 'org.apache.commons.dbcp.BasicDataSource' instances. These use a resource pool of type 'org.apache.commons.pool.impl.GenericObjectPool'. Finally, for this type of pool we can demand that objects be verified at borrowing time - which is what we want as it will prevent Tomcat getting its paws on stale database connections.

Wiki Markup
*3*: Configuring the attributes of the 'BasicDataSourceFactory'. The allowed attributes can be found by looking for JavaBean-compliant set() methods and members in the source or the
\[http://jakarta.apache.org/commons/dbcp/apidocs/index.html DBCP API doc\]. In particular: what driver shall be used by the factory to actually get database connections: 'com.mysql.jdbc.Driver'.

Additionally (and one level down, if you will) the 'Driver' named in '!driverClassName' is itself configured through the URL used when a new connection is created

Wiki Markup
Following the \[http://issues.apache.org/bugzilla/show_bug.cgi?id=24723 bug 24723\] I have put the 'Resource' definition into the 'GlobalNamingResource' instead of the 'Context' definition. Where it should be according to the documentation.

  • Wiki Markup
    \[http://jakarta.apache.org/tomcat/tomcat-5.5-doc/config/server.html Tomcat 5.5 'server' element configuration\].
  • Wiki Markup
    \[http://jakarta.apache.org/tomcat/tomcat-5.5-doc/jndi-datasource-examples-howto.html Tomcat 5.5 JNDI-DataSource examples\]
  • Wiki Markup
    \[http://jakarta.apache.org/tomcat/tomcat-5.5-doc/config/context.html#Resource%20Definitions The definition of 'resource'\]
  • Wiki Markup
    \[http://jakarta.apache.org/tomcat/tomcat-4.1-doc/jndi-resources-howto.html Tomcat 4.1 JDNI resources howto\]
  • Wiki Markup
    \[http://forums.devshed.com/archive/t-120081 (Outside link) Another trouble report\]
    \\
No Format

<Server ...>

    <GlobalNamingResources>

        <Resource name="jdbc/mydatabase"
                  auth="Container"
                  type="javax.sql.DataSource"
                  factory="org.apache.commons.dbcp.BasicDataSourceFactory"
                  driverClassName="com.mysql.jdbc.Driver"
                  validationQuery="SELECT 1"
                  loginTimeout="10"
                  maxWait="5000"
                  username="i_am_tomcat"
                  password="my_password_is_foo"
                  testOnBorrow="true"
                  url="jdbc:mysql://127.0.0.1/mydatabase?connectTimeout=5000&amp;socketTimeout=8000&amp;useUsageAdvisor=true"
        />

    </GlobalNamingResources>

    ...something something...

</Server>

Now define the Tomcat Authentication Realm. I have done that inside the Host tag, because I could not make it work inside the Context tag for some reason. Tomcat gave an exception in that case.

The Realm implementation is 'org.apache.catalina.realm.DataSourceRealm'; it will use a 'javax.sql.DataSource' interface found in the JNDI initial context. The location of that interface inside the JNDI namespace is given by 'dataSourceName': 'java:com/env/jdbc/mydatabase'.

  • Wiki Markup
    \[http://jakarta.apache.org/tomcat/tomcat-5.5-doc/config/realm.html Tomcat 5.5 Realm configuration\]
  • Wiki Markup
    \[http://jakarta.apache.org/tomcat/tomcat-5.5-doc/realm-howto.html#DataSourceRealm Tomcat 5.5 DataSourceRealm\]
    \\
No Format

<Host ....>

  <Context ...> ... </Context>
 
  <Context ...> ... </Context>

  <Realm className="org.apache.catalina.realm.DataSourceRealm"
                    dataSourceName="jdbc/mydatabase"
                    digest="MD5"
                    roleNameCol="web_user_role_name"
                    userCredCol="account_md5_password"
                    userNameCol="account_login"
                    userRoleTable="web_user_role_t"
                    userTable="account_t" />

</Host>

Wiki Markup
Finally, beware \[http://issues.apache.org/bugzilla/show_bug.cgi?id=33357 bug 33357\] in 5.5.7 which should be fixed soon though :-P

Hibernate

Wiki Markup
\[http://www.hibernate.org/ Hibernate\] is a powerful, ultra-high performance object/relational persistence and query service for Java. Hibernate lets you develop persistent classes following common Java idiom - including association, inheritance, polymorphism, composition and the Java collections framework.

...