Date: 2005-03-09T16:47:27 Editor: DavidTonhofer Wiki: Jakarta Commons Wiki Page: DBCP URL: http://wiki.apache.org/jakarta-commons/DBCP
no comment Change Log: ------------------------------------------------------------------------------ @@ -12,10 +12,9 @@ Diagrams hosted by http://rei1.m-plify.net -= Tomcat Configuration examples = - -Some Tomcat JNDI Datasource examples (in addition to the [http://jakarta.apache.org/tomcat/tomcat-5.0-doc/jndi-datasource-examples-howto.html Tomcat howto]). += Tomcat 5.0 Configuration examples = +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''' {{{ @@ -53,6 +52,103 @@ <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. + +'''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 + +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. + + * [http://jakarta.apache.org/tomcat/tomcat-5.5-doc/config/server.html Tomcat 5.5 'server' element configuration]. + * [http://jakarta.apache.org/tomcat/tomcat-5.5-doc/jndi-datasource-examples-howto.html Tomcat 5.5 JNDI-DataSource examples] + * [http://jakarta.apache.org/tomcat/tomcat-5.5-doc/config/context.html#Resource%20Definitions The definition of 'resource'] + * [http://jakarta.apache.org/tomcat/tomcat-4.1-doc/jndi-resources-howto.html Tomcat 4.1 JDNI resources howto] + * [http://forums.devshed.com/archive/t-120081 (Outside link) Another trouble report] + +{{{ +<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&socketTimeout=8000&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'. + + * [http://jakarta.apache.org/tomcat/tomcat-5.5-doc/config/realm.html Tomcat 5.5 Realm configuration] + * [http://jakarta.apache.org/tomcat/tomcat-5.5-doc/realm-howto.html#DataSourceRealm Tomcat 5.5 DataSourceRealm] + +{{{ +<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> +}}} + +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 = [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. --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]