Return-Path: Delivered-To: apmail-db-derby-commits-archive@www.apache.org Received: (qmail 80192 invoked from network); 13 Feb 2008 12:27:16 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 13 Feb 2008 12:27:16 -0000 Received: (qmail 37290 invoked by uid 500); 13 Feb 2008 12:27:10 -0000 Delivered-To: apmail-db-derby-commits-archive@db.apache.org Received: (qmail 37267 invoked by uid 500); 13 Feb 2008 12:27:10 -0000 Mailing-List: contact derby-commits-help@db.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: Reply-To: "Derby Development" List-Id: Delivered-To: mailing list derby-commits@db.apache.org Received: (qmail 37256 invoked by uid 99); 13 Feb 2008 12:27:09 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 13 Feb 2008 04:27:09 -0800 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 13 Feb 2008 12:26:31 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 03E4D1A9832; Wed, 13 Feb 2008 04:26:51 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r627380 - in /db/derby/code/trunk/java: client/org/apache/derby/jdbc/ engine/org/apache/derby/loc/ shared/org/apache/derby/shared/common/reference/ testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ Date: Wed, 13 Feb 2008 12:26:47 -0000 To: derby-commits@db.apache.org From: kristwaa@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20080213122651.03E4D1A9832@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: kristwaa Date: Wed Feb 13 04:26:40 2008 New Revision: 627380 URL: http://svn.apache.org/viewvc?rev=627380&view=rev Log: DERBY-3325: Add 'maxStatements' property to ClientConnectionPoolDataSource. The patch adds the maxStatements property, but it will not have any effect before DERBY-3329 has been committed. Adjusted the DataSourceReferenceTest to account for the new property, and added a simple test that tests that the property is handled correctly in the data source. Patch file: derby-3325-1b-maxStatements.diff Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ClientConnectionPoolDataSourceTest.java (with props) Modified: db/derby/code/trunk/java/client/org/apache/derby/jdbc/ClientBaseDataSource.java db/derby/code/trunk/java/client/org/apache/derby/jdbc/ClientConnectionPoolDataSource.java db/derby/code/trunk/java/engine/org/apache/derby/loc/messages.xml db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/MessageId.java db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/DataSourceReferenceTest.java Modified: db/derby/code/trunk/java/client/org/apache/derby/jdbc/ClientBaseDataSource.java URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/jdbc/ClientBaseDataSource.java?rev=627380&r1=627379&r2=627380&view=diff ============================================================================== --- db/derby/code/trunk/java/client/org/apache/derby/jdbc/ClientBaseDataSource.java (original) +++ db/derby/code/trunk/java/client/org/apache/derby/jdbc/ClientBaseDataSource.java Wed Feb 13 04:26:40 2008 @@ -1056,6 +1056,24 @@ return this.traceFileAppend; } + /** + * Returns the maximum number of JDBC prepared statements a connection is + * allowed to cache. + *

+ * A basic data source will always return zero. If statement caching is + * required, use a {@link javax.sql.ConnectionPoolDataSource}. + *

+ * This method is used internally by Derby to determine if statement + * pooling is to be enabled or not. + * + * @return Maximum number of statements to cache, or 0 if + * caching is disabled (default). + * + * @see ClientConnectionPoolDataSource + */ + public int maxStatementsToPool() { + return 0; + } // --- private helper methods Modified: db/derby/code/trunk/java/client/org/apache/derby/jdbc/ClientConnectionPoolDataSource.java URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/jdbc/ClientConnectionPoolDataSource.java?rev=627380&r1=627379&r2=627380&view=diff ============================================================================== --- db/derby/code/trunk/java/client/org/apache/derby/jdbc/ClientConnectionPoolDataSource.java (original) +++ db/derby/code/trunk/java/client/org/apache/derby/jdbc/ClientConnectionPoolDataSource.java Wed Feb 13 04:26:40 2008 @@ -21,12 +21,15 @@ package org.apache.derby.jdbc; +import java.io.IOException; +import java.io.ObjectInputStream; import java.sql.SQLException; import javax.sql.ConnectionPoolDataSource; -import javax.sql.DataSource; import javax.sql.PooledConnection; import org.apache.derby.client.am.LogWriter; import org.apache.derby.client.am.SqlException; +import org.apache.derby.shared.common.i18n.MessageUtil; +import org.apache.derby.shared.common.reference.MessageId; /** * ClientConnectionPoolDataSource is a factory for PooledConnection objects. @@ -44,8 +47,22 @@ public class ClientConnectionPoolDataSource extends ClientDataSource implements ConnectionPoolDataSource { private static final long serialVersionUID = -539234282156481377L; + /** Message utility used to obtain localized messages. */ + private static final MessageUtil msgUtil = + new MessageUtil("org.apache.derby.loc.clientmessages"); public static final String className__ = "org.apache.derby.jdbc.ClientConnectionPoolDataSource"; + /** + * Specifies the maximum number of statements that can be cached per + * connection by the JDBC driver. + *

+ * A value of 0 disables statement caching, negative values + * are not allowed. The default is that caching is disabled. + * + * @serial + */ + private int maxStatements = 0; + public ClientConnectionPoolDataSource() { super(); } @@ -100,4 +117,73 @@ return ClientDriver.getFactory().newClientPooledConnection(ds, dncLogWriter, user, password); } + + /** + * Specifies the maximum size of the statement cache. + * + * @param maxStatements maximum number of cached statements + * + * @throws IllegalArgumentException if maxStatements is + * negative + */ + public void setMaxStatements(int maxStatements) { + // Disallow negative values. + if (maxStatements < 0) { + throw new IllegalArgumentException(msgUtil.getTextMessage( + MessageId.CONN_NEGATIVE_MAXSTATEMENTS, + new Integer(maxStatements))); + } + this.maxStatements = maxStatements; + } + + /** + * Returns the maximum number of JDBC prepared statements a connection is + * allowed to cache. + * + * @return Maximum number of statements to cache, or 0 if + * caching is disabled (default). + */ + public int getMaxStatements() { + return this.maxStatements; + } + + /** + * Internally used method. + * + * @see ClientBaseDataSource#maxStatementsToPool + */ + public int maxStatementsToPool() { + return this.maxStatements; + } + + /** + * Make sure the state of the de-serialized object is valid. + */ + private final void validateState() { + // Make sure maxStatements is zero or higher. + if (maxStatements < 0) { + throw new IllegalArgumentException(msgUtil.getTextMessage( + MessageId.CONN_NEGATIVE_MAXSTATEMENTS, + new Integer(maxStatements))); + } + } + + /** + * Read an object from the ObjectInputStream. + *

+ * This implementation differs from the default one by initiating state + * validation of the object created. + * + * @param inputStream data stream to read objects from + * @throws ClassNotFoundException if instantiating a class fails + * @throws IOException if reading from the stream fails + */ + private void readObject(ObjectInputStream inputStream) + throws ClassNotFoundException, IOException { + // Always perform the default de-serialization first + inputStream.defaultReadObject(); + + // Ensure that object state has not been corrupted or tampered with. + validateState(); + } } Modified: db/derby/code/trunk/java/engine/org/apache/derby/loc/messages.xml URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/loc/messages.xml?rev=627380&r1=627379&r2=627380&view=diff ============================================================================== --- db/derby/code/trunk/java/engine/org/apache/derby/loc/messages.xml (original) +++ db/derby/code/trunk/java/engine/org/apache/derby/loc/messages.xml Wed Feb 13 04:26:40 2008 @@ -7459,6 +7459,12 @@ The user's password for the connection + + J134 + Negative values for the maxStatements property are not allowed: {0} + maxStatements + + Modified: db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/MessageId.java URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/MessageId.java?rev=627380&r1=627379&r2=627380&view=diff ============================================================================== --- db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/MessageId.java (original) +++ db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/MessageId.java Wed Feb 13 04:26:40 2008 @@ -147,6 +147,11 @@ String CONN_DRDA_DATASTREAM_SYNTAX_ERROR = "J131"; String CONN_USERNAME_DESCRIPTION = "J132"; String CONN_PASSWORD_DESCRIPTION = "J133"; + /** + * Error message for negative values for the maxStatements property + * of data sources supporting statement pooling. + */ + String CONN_NEGATIVE_MAXSTATEMENTS = "J134"; /* ** Authentication Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ClientConnectionPoolDataSourceTest.java URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ClientConnectionPoolDataSourceTest.java?rev=627380&view=auto ============================================================================== --- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ClientConnectionPoolDataSourceTest.java (added) +++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ClientConnectionPoolDataSourceTest.java Wed Feb 13 04:26:40 2008 @@ -0,0 +1,158 @@ +/* + + Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.ClientConnectionPoolDataSourceTest + + 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.derbyTesting.functionTests.tests.jdbcapi; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +import javax.sql.PooledConnection; + +import junit.framework.Test; + +import org.apache.derby.jdbc.ClientConnectionPoolDataSource; +import org.apache.derbyTesting.junit.BaseJDBCTestCase; +import org.apache.derbyTesting.junit.J2EEDataSource; +import org.apache.derbyTesting.junit.JDBC; +import org.apache.derbyTesting.junit.TestConfiguration; + +/** + * Basic tests of the ConnectionPoolDataSource in the client + * driver. + */ +public class ClientConnectionPoolDataSourceTest + extends BaseJDBCTestCase { + + public ClientConnectionPoolDataSourceTest(String name) { + super(name); + } + + /** + * Verify that handling of the maxStatements property is + * working. + */ + public void testMaxStatementsProperty() { + ClientConnectionPoolDataSource cDs = + new ClientConnectionPoolDataSource(); + // Check the default value. + assertEquals("Unexpected default value", 0, cDs.getMaxStatements()); + cDs.setMaxStatements(25); + // Verify that the new value has been set. + assertEquals("New value not set", 25, cDs.getMaxStatements()); + // Try a negative value + try { + cDs.setMaxStatements(-99); + fail("Negative values should not be allowed: " + + cDs.getMaxStatements()); + } catch (IllegalArgumentException iae) { + // As expected, continue the test. + } + // Try setting it to zero to disable statement pooling. + cDs.setMaxStatements(0); + assertEquals("New value not set", 0, cDs.getMaxStatements()); + } + + /** + * Tests basic connectivity when connection is obtained from a connection + * pool data source without statement pooling. + * + * @throws SQLException if database operations fail + */ + public void testGetConnectionNoStatementPooling() + throws SQLException { + ClientConnectionPoolDataSource cDs = (ClientConnectionPoolDataSource) + J2EEDataSource.getConnectionPoolDataSource(); + // Make sure statement pooling is disabled. + cDs.setMaxStatements(0); + assertEquals(0, cDs.getMaxStatements()); + verifyConnection(cDs); + } + + /** + * Tests basic connectivity when connection is obtained from a connection + * pool data source with statement pooling enabled. + * + * @throws SQLException if database operations fail + */ + public void testGetConnectionWithStatementPooling() + throws SQLException { + ClientConnectionPoolDataSource cDs = (ClientConnectionPoolDataSource) + J2EEDataSource.getConnectionPoolDataSource(); + // Enable statement pooling. + cDs.setMaxStatements(27); + assertTrue(cDs.getMaxStatements() > 0); + verifyConnection(cDs); + } + + /** + * Do some basic verification on a connection obtained from the data source. + * + * @param cDs data source to get connection from + * @throws SQLException if a JDBC operation fails + */ + private void verifyConnection(ClientConnectionPoolDataSource cDs) + throws SQLException { + PooledConnection pc = null; + // Workaround for "bug" in the JUnit framework, where data source + // connections do not create the database if it does not exist. + // See DERBY-3306 for more information. + try { + pc = cDs.getPooledConnection(); + } catch (SQLException sqle) { + assertSQLState("08004", sqle); + getConnection(); + pc = cDs.getPooledConnection(); + } + // Get a connection and make sure we can access the database. + Connection con = pc.getConnection(); + Statement stmt = con.createStatement(); + ResultSet rs = stmt.executeQuery("select * from sys.systables"); + JDBC.assertDrainResultsHasData(rs); + PreparedStatement ps1 = con.prepareStatement("values 31"); + JDBC.assertSingleValueResultSet(ps1.executeQuery(), "31"); + ps1.close(); + PreparedStatement ps2 = con.prepareStatement("values 31"); + // The physical statement is supposed to be the same, but not the + // logical prepared statements (if pooling is used). + assertNotSame(ps1, ps2); + JDBC.assertSingleValueResultSet(ps2.executeQuery(), "31"); + // Close everything + stmt.close(); + ps2.close(); + con.close(); + pc.close(); + } + + /** + * Returns a suite that will run only in the client-server configuration. + * + * @return A client-server suite with all the tests. + */ + public static Test suite() { + // The tests are run in the client-server configuration only, because + // the code being tests does not exist in the embedded driver. + return TestConfiguration.clientServerSuite( + ClientConnectionPoolDataSourceTest.class); + } +} Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ClientConnectionPoolDataSourceTest.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/DataSourceReferenceTest.java URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/DataSourceReferenceTest.java?rev=627380&r1=627379&r2=627380&view=diff ============================================================================== --- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/DataSourceReferenceTest.java (original) +++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/DataSourceReferenceTest.java Wed Feb 13 04:26:40 2008 @@ -118,6 +118,14 @@ BASE_CLIENT_DS.addProperty("traceDirectory", "XX_traceDirectory_1476"); } + /** Descriptor for the client connection pool data source. */ + private static final DataSourceDescriptor POOL_CLIENT_DS = + new DataSourceDescriptor("Connection pool client data source", + BASE_CLIENT_DS); + + static { + POOL_CLIENT_DS.addProperty("maxStatements", "10", "0"); + } /** * Creates a new fixture. @@ -171,7 +179,7 @@ // Specify client data source descriptors. descriptors = new DataSourceDescriptor[] { BASE_CLIENT_DS, // Base - BASE_CLIENT_DS, // Pool + POOL_CLIENT_DS, // Pool BASE_CLIENT_DS // XA }; } else {