You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by fh...@apache.org on 2008/11/12 22:50:08 UTC

svn commit: r713522 - in /tomcat/trunk/modules/jdbc-pool: doc/ java/org/apache/tomcat/jdbc/pool/ test/org/apache/tomcat/jdbc/test/

Author: fhanik
Date: Wed Nov 12 13:50:03 2008
New Revision: 713522

URL: http://svn.apache.org/viewvc?rev=713522&view=rev
Log:
Updated documentation and added wrappers for toString and added an example

Added:
    tomcat/trunk/modules/jdbc-pool/test/org/apache/tomcat/jdbc/test/SimplePOJOExample.java
Modified:
    tomcat/trunk/modules/jdbc-pool/doc/jdbc-pool.xml
    tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/JdbcInterceptor.java
    tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/PooledConnection.java
    tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/ProxyConnection.java

Modified: tomcat/trunk/modules/jdbc-pool/doc/jdbc-pool.xml
URL: http://svn.apache.org/viewvc/tomcat/trunk/modules/jdbc-pool/doc/jdbc-pool.xml?rev=713522&r1=713521&r2=713522&view=diff
==============================================================================
--- tomcat/trunk/modules/jdbc-pool/doc/jdbc-pool.xml (original)
+++ tomcat/trunk/modules/jdbc-pool/doc/jdbc-pool.xml Wed Nov 12 13:50:03 2008
@@ -338,6 +338,98 @@
   </subsection>
 </section>
 
+<section name="Code Example">
+  <p>Other examples of Tomcat configuration for JDBC usage can be found <a href="http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html">in the Tomcat documentation</a>. </p>
+  <subsection name="Plain Ol' Java">
+    <p>Here is a simple example of how to create and use a data source.</p> 
+    <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) {}
+                }  
+            }
+        
+        }
+    </source>
+  </subsection>
+  <subsection name="As a Resource">
+    <p>And here is an example on how to configure a resource for JNDI lookups</p>
+    <source>
+    &lt;Resource name=&quot;jdbc/TestDB&quot; 
+              auth=&quot;Container&quot; 
+              type=&quot;javax.sql.DataSource&quot; 
+              factory=&quot;org.apache.tomcat.jdbc.pool.DataSourceFactory&quot;
+              testWhileIdle=&quot;true&quot;
+              testOnBorrow=&quot;true&quot;
+              testOnReturn=&quot;false&quot;
+              validationQuery=&quot;SELECT 1&quot;
+              validationInterval=&quot;30000&quot;
+              timeBetweenEvictionRunsMillis=&quot;30000&quot;
+              maxActive=&quot;100&quot; 
+              minIdle=&quot;10&quot; 
+              maxWait=&quot;10000&quot; 
+              initialSize=&quot;10&quot;
+              removeAbandonedTimeout=&quot;60&quot;
+              removeAbandoned=&quot;true&quot;
+              logAbandoned=&quot;true&quot;
+              minEvictableIdleTimeMillis=&quot;30000&quot; 
+              jmxEnabled=&quot;true&quot;
+              username=&quot;root&quot; 
+              password=&quot;password&quot; 
+              driverClassName=&quot;com.mysql.jdbc.Driver&quot;
+              url=&quot;jdbc:mysql://localhost:3306/mysql?autoReconnect=true&quot;/&gt;
+
+    
+    </source>
+  
+  </subsection>
+  
+</section>
+
+
 </body>
 
 </document>

Modified: tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/JdbcInterceptor.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/JdbcInterceptor.java?rev=713522&r1=713521&r2=713522&view=diff
==============================================================================
--- tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/JdbcInterceptor.java (original)
+++ tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/JdbcInterceptor.java Wed Nov 12 13:50:03 2008
@@ -25,6 +25,7 @@
  */
 public abstract class JdbcInterceptor implements InvocationHandler {
     public  static final String CLOSE_VAL = "close";
+    public  static final String TOSTRING_VAL = "toString";
 
     private JdbcInterceptor next = null;
 

Modified: tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/PooledConnection.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/PooledConnection.java?rev=713522&r1=713521&r2=713522&view=diff
==============================================================================
--- tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/PooledConnection.java (original)
+++ tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/PooledConnection.java Wed Nov 12 13:50:03 2008
@@ -300,5 +300,9 @@
             this.handler = new WeakReference<JdbcInterceptor>(handler);
         }
     }
+    
+    public String toString() {
+        return "PooledConnection["+(connection!=null?connection.toString():"null")+"]";
+    }
 
 }

Modified: tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/ProxyConnection.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/ProxyConnection.java?rev=713522&r1=713521&r2=713522&view=diff
==============================================================================
--- tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/ProxyConnection.java (original)
+++ tomcat/trunk/modules/jdbc-pool/java/org/apache/tomcat/jdbc/pool/ProxyConnection.java Wed Nov 12 13:50:03 2008
@@ -74,6 +74,8 @@
             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 ConnectionPool getParentPool() {
         return pool;
     }
+    
+    public String toString() {
+        return "ProxyConnection["+(connection!=null?connection.toString():"null")+"]";
+    }
 
 }

Added: tomcat/trunk/modules/jdbc-pool/test/org/apache/tomcat/jdbc/test/SimplePOJOExample.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/modules/jdbc-pool/test/org/apache/tomcat/jdbc/test/SimplePOJOExample.java?rev=713522&view=auto
==============================================================================
--- tomcat/trunk/modules/jdbc-pool/test/org/apache/tomcat/jdbc/test/SimplePOJOExample.java (added)
+++ tomcat/trunk/modules/jdbc-pool/test/org/apache/tomcat/jdbc/test/SimplePOJOExample.java Wed Nov 12 13:50:03 2008
@@ -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) {}
+        }  
+    }
+
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org