From: fhanik Date: Wed, 12 Nov 2008 21:50:03 +0000 (+0000) Subject: Updated documentation and added wrappers for toString and added an example X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=8a2dec91c9e80316e943c297e464766e738d5d0d;p=tomcat7.0 Updated documentation and added wrappers for toString and added an example git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@713522 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/modules/jdbc-pool/doc/jdbc-pool.xml b/modules/jdbc-pool/doc/jdbc-pool.xml index cd68836a0..9bc11debb 100644 --- a/modules/jdbc-pool/doc/jdbc-pool.xml +++ b/modules/jdbc-pool/doc/jdbc-pool.xml @@ -338,6 +338,98 @@ +
+

Other examples of Tomcat configuration for JDBC usage can be found in the Tomcat documentation.

+ +

Here is a simple example of how to create and use a data source.

+ + import java.sql.Connection; + import java.sql.ResultSet; + import java.sql.Statement; + + import org.apache.tomcat.jdbc.pool.DataSource; + import org.apache.tomcat.jdbc.pool.PoolProperties; + + public class SimplePOJOExample { + + public static void main(String[] args) throws Exception { + PoolProperties p = new PoolProperties(); + p.setUrl("jdbc:mysql://localhost:3306/mysql?autoReconnect=true"); + p.setDriverClassName("com.mysql.jdbc.Driver"); + p.setUsername("root"); + p.setPassword("password"); + p.setJmxEnabled(true); + p.setTestWhileIdle(false); + p.setTestOnBorrow(true); + p.setValidationQuery("SELECT 1"); + p.setTestOnReturn(false); + p.setValidationInterval(30000); + p.setTimeBetweenEvictionRunsMillis(30000); + p.setMaxActive(100); + p.setInitialSize(10); + p.setMaxWait(10000); + p.setRemoveAbandonedTimeout(60); + p.setMinEvictableIdleTimeMillis(30000); + p.setMinIdle(10); + p.setLogAbandoned(true); + p.setRemoveAbandoned(true); + DataSource datasource = new DataSource(); + datasource.setPoolProperties(p); + + Connection con = null; + try { + con = datasource.getConnection(); + Statement st = con.createStatement(); + ResultSet rs = st.executeQuery("select * from user"); + int cnt = 1; + while (rs.next()) { + System.out.println((cnt++)+". Host:" +rs.getString("Host")+" User:"+rs.getString("User")+" Password:"+rs.getString("Password")); + } + rs.close(); + st.close(); + } finally { + if (con!=null) try {con.close();}catch (Exception ignore) {} + } + } + + } + +
+ +

And here is an example on how to configure a resource for JNDI lookups

+ + <Resource name="jdbc/TestDB" + auth="Container" + type="javax.sql.DataSource" + factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" + testWhileIdle="true" + testOnBorrow="true" + testOnReturn="false" + validationQuery="SELECT 1" + validationInterval="30000" + timeBetweenEvictionRunsMillis="30000" + maxActive="100" + minIdle="10" + maxWait="10000" + initialSize="10" + removeAbandonedTimeout="60" + removeAbandoned="true" + logAbandoned="true" + minEvictableIdleTimeMillis="30000" + jmxEnabled="true" + username="root" + password="password" + driverClassName="com.mysql.jdbc.Driver" + url="jdbc:mysql://localhost:3306/mysql?autoReconnect=true"/> + + + + +
+ +
+ + diff --git a/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/JdbcInterceptor.java b/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/JdbcInterceptor.java index b48bf04c6..21e26b859 100644 --- a/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/JdbcInterceptor.java +++ b/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/JdbcInterceptor.java @@ -25,6 +25,7 @@ import java.lang.reflect.Method; */ public abstract class JdbcInterceptor implements InvocationHandler { public static final String CLOSE_VAL = "close"; + public static final String TOSTRING_VAL = "toString"; private JdbcInterceptor next = null; diff --git a/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/PooledConnection.java b/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/PooledConnection.java index 27767dd91..364eeeeaf 100644 --- a/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/PooledConnection.java +++ b/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/PooledConnection.java @@ -300,5 +300,9 @@ public class PooledConnection { this.handler = new WeakReference(handler); } } + + public String toString() { + return "PooledConnection["+(connection!=null?connection.toString():"null")+"]"; + } } diff --git a/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/ProxyConnection.java b/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/ProxyConnection.java index feba58f5f..5aa2cf209 100644 --- a/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/ProxyConnection.java +++ b/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/ProxyConnection.java @@ -74,6 +74,8 @@ public class ProxyConnection extends JdbcInterceptor { this.connection = null; pool.returnConnection(poolc); return null; + } else if (TOSTRING_VAL==method.getName()) { + return this.toString(); } return method.invoke(connection.getConnection(),args); } @@ -89,5 +91,9 @@ public class ProxyConnection extends JdbcInterceptor { public ConnectionPool getParentPool() { return pool; } + + public String toString() { + return "ProxyConnection["+(connection!=null?connection.toString():"null")+"]"; + } } diff --git a/modules/jdbc-pool/test/org/apache/tomcat/jdbc/test/SimplePOJOExample.java b/modules/jdbc-pool/test/org/apache/tomcat/jdbc/test/SimplePOJOExample.java new file mode 100644 index 000000000..629840750 --- /dev/null +++ b/modules/jdbc-pool/test/org/apache/tomcat/jdbc/test/SimplePOJOExample.java @@ -0,0 +1,68 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tomcat.jdbc.test; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; + +import org.apache.tomcat.jdbc.pool.DataSource; +import org.apache.tomcat.jdbc.pool.PoolProperties; + +public class SimplePOJOExample { + + public static void main(String[] args) throws Exception { + PoolProperties p = new PoolProperties(); + p.setUrl("jdbc:mysql://localhost:3306/mysql?autoReconnect=true"); + p.setDriverClassName("com.mysql.jdbc.Driver"); + p.setUsername("root"); + p.setPassword("password"); + p.setJmxEnabled(true); + p.setTestWhileIdle(false); + p.setTestOnBorrow(true); + p.setValidationQuery("SELECT 1"); + p.setTestOnReturn(false); + p.setValidationInterval(30000); + p.setTimeBetweenEvictionRunsMillis(30000); + p.setMaxActive(100); + p.setInitialSize(10); + p.setMaxWait(10000); + p.setRemoveAbandonedTimeout(60); + p.setMinEvictableIdleTimeMillis(30000); + p.setMinIdle(10); + p.setLogAbandoned(true); + p.setRemoveAbandoned(true); + DataSource datasource = new DataSource(); + datasource.setPoolProperties(p); + + Connection con = null; + try { + con = datasource.getConnection(); + Statement st = con.createStatement(); + ResultSet rs = st.executeQuery("select * from user"); + int cnt = 1; + while (rs.next()) { + System.out.println((cnt++)+". Host:" +rs.getString("Host")+" User:"+rs.getString("User")+" Password:"+rs.getString("Password")); + } + rs.close(); + st.close(); + } finally { + if (con!=null) try {con.close();}catch (Exception ignore) {} + } + } + +}