[jboss-cvs] JBossAS SVN: r63806 - in branches/JBoss_3_2_6_CP: testsuite/src/main/org/jboss/test/jca/ejb and 3 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Jul 3 19:53:31 EDT 2007


Author: vivekl at redhat.com
Date: 2007-07-03 19:53:31 -0400 (Tue, 03 Jul 2007)
New Revision: 63806

Added:
   branches/JBoss_3_2_6_CP/testsuite/src/main/org/jboss/test/jca/jdbc/TestPreparedStatement.java
Modified:
   branches/JBoss_3_2_6_CP/connector/src/main/org/jboss/resource/adapter/jdbc/CachedPreparedStatement.jpp
   branches/JBoss_3_2_6_CP/testsuite/src/main/org/jboss/test/jca/ejb/JDBCStatementTestsConnectionSessionBean.java
   branches/JBoss_3_2_6_CP/testsuite/src/main/org/jboss/test/jca/jdbc/TestConnection.java
   branches/JBoss_3_2_6_CP/testsuite/src/main/org/jboss/test/jca/jdbc/TestStatement.java
   branches/JBoss_3_2_6_CP/testsuite/src/main/org/jboss/test/jca/test/JDBCStatementTestsConnectionUnitTestCase.java
   branches/JBoss_3_2_6_CP/testsuite/src/resources/jca/jdbc/testdriver-ds.xml
Log:
ASPATCH-230: JBAS-2887 - Reset cached prepared statement attributes



Modified: branches/JBoss_3_2_6_CP/connector/src/main/org/jboss/resource/adapter/jdbc/CachedPreparedStatement.jpp
===================================================================
--- branches/JBoss_3_2_6_CP/connector/src/main/org/jboss/resource/adapter/jdbc/CachedPreparedStatement.jpp	2007-07-03 21:07:54 UTC (rev 63805)
+++ branches/JBoss_3_2_6_CP/connector/src/main/org/jboss/resource/adapter/jdbc/CachedPreparedStatement.jpp	2007-07-03 23:53:31 UTC (rev 63806)
@@ -43,9 +43,32 @@
    private SynchronizedBoolean cached = new SynchronizedBoolean(true);
    private SynchronizedInt inUse = new SynchronizedInt(1);
 
-   public CachedPreparedStatement(PreparedStatement ps)
+   private int defaultMaxFieldSize;
+   private int defaultMaxRows;
+   private int defaultQueryTimeout;
+   private int defaultFetchDirection;
+   private int defaultFetchSize;
+   private int currentMaxFieldSize;
+   private int currentMaxRows;
+   private int currentQueryTimeout;
+   private int currentFetchDirection;
+   private int currentFetchSize;
+
+   public CachedPreparedStatement(PreparedStatement ps) throws SQLException
    {
       this.ps = ps;
+
+      // Remember the defaults
+      defaultMaxFieldSize = ps.getMaxFieldSize();
+      defaultMaxRows = ps.getMaxRows();
+      defaultQueryTimeout = ps.getQueryTimeout();
+      defaultFetchDirection = ResultSet.FETCH_FORWARD;
+      defaultFetchSize = 0;
+      currentMaxFieldSize = defaultMaxFieldSize;
+      currentMaxRows = defaultMaxRows;
+      currentQueryTimeout = defaultQueryTimeout;
+      currentFetchDirection = defaultFetchDirection;
+      currentFetchSize = defaultFetchSize;
    }
 
    public PreparedStatement getUnderlyingPreparedStatement()
@@ -290,14 +313,46 @@
    }
    
    /**
-    * Does nothing; this statement is only closed when the connection is closed
+    * This statement is only closed when the connection is closed
     * or PreparedStatementCache.ageOut() closes the underlying connection.
     */
    public void close() throws SQLException
    {
       inUse.decrement();
-      if (inUse.get() == 0 && cached.get() == false)
-         ps.close();
+      if (inUse.get() == 0)
+      {
+         if (cached.get() == false)
+            ps.close();
+         else
+         {
+            // Reset the defaults
+            if (defaultMaxFieldSize != currentMaxFieldSize)
+            {
+               ps.setMaxFieldSize(defaultMaxFieldSize);
+               currentMaxFieldSize = defaultMaxFieldSize;
+            }
+            if (defaultMaxRows != currentMaxRows)
+            {
+               ps.setMaxRows(defaultMaxRows);
+               currentMaxRows = defaultMaxRows;
+            }
+            if (defaultQueryTimeout != currentQueryTimeout)
+            {
+               ps.setQueryTimeout(defaultQueryTimeout);
+               currentQueryTimeout = defaultQueryTimeout;
+            }
+            if (defaultFetchDirection != currentFetchDirection)
+            {
+               ps.setFetchDirection(defaultFetchDirection);
+               currentFetchDirection = defaultFetchDirection;
+            }
+            if (defaultFetchSize != currentFetchSize)
+            {
+               ps.setFetchSize(defaultFetchSize);
+               currentFetchSize = defaultFetchSize;
+            }
+         }
+      }
    }
 
    public int getMaxFieldSize() throws SQLException
@@ -308,6 +363,7 @@
    public void setMaxFieldSize(int max) throws SQLException
    {
       ps.setMaxFieldSize(max);
+      currentMaxFieldSize = max;
    }
 
    public int getMaxRows() throws SQLException
@@ -318,6 +374,7 @@
    public void setMaxRows(int max) throws SQLException
    {
       ps.setMaxRows(max);
+      currentMaxRows = max;
    }
 
    public void setEscapeProcessing(boolean enable) throws SQLException
@@ -333,6 +390,7 @@
    public void setQueryTimeout(int seconds) throws SQLException
    {
       ps.setQueryTimeout(seconds);
+      currentQueryTimeout = seconds;
    }
 
    public void cancel() throws SQLException
@@ -378,6 +436,7 @@
    public void setFetchDirection(int direction) throws SQLException
    {
       ps.setFetchDirection(direction);
+      currentFetchDirection = direction;
    }
 
    public int getFetchDirection() throws SQLException
@@ -388,6 +447,7 @@
    public void setFetchSize(int rows) throws SQLException
    {
       ps.setFetchSize(rows);
+      currentFetchSize = rows;
    }
 
    public int getFetchSize() throws SQLException

Modified: branches/JBoss_3_2_6_CP/testsuite/src/main/org/jboss/test/jca/ejb/JDBCStatementTestsConnectionSessionBean.java
===================================================================
--- branches/JBoss_3_2_6_CP/testsuite/src/main/org/jboss/test/jca/ejb/JDBCStatementTestsConnectionSessionBean.java	2007-07-03 21:07:54 UTC (rev 63805)
+++ branches/JBoss_3_2_6_CP/testsuite/src/main/org/jboss/test/jca/ejb/JDBCStatementTestsConnectionSessionBean.java	2007-07-03 23:53:31 UTC (rev 63806)
@@ -8,17 +8,22 @@
 
 package org.jboss.test.jca.ejb;
 
+import java.security.CodeSource;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+
 import javax.ejb.SessionBean;
 import javax.naming.InitialContext;
+import javax.naming.NamingException;
 import javax.sql.DataSource;
-import java.sql.Connection;
-import java.sql.SQLException;
-import java.security.CodeSource;
 import javax.ejb.EJBException;
 import javax.ejb.SessionContext;
-import javax.naming.NamingException;
 
 import org.jboss.resource.adapter.jdbc.WrappedConnection;
+import org.jboss.ejb.plugins.cmp.jdbc.WrappedStatement;
 import org.jboss.test.jca.jdbc.TestConnection;
 import org.jboss.logging.Logger;
 
@@ -103,6 +108,82 @@
 
    }
 
+   /**
+    * Test cached prepared statement gets reset
+    *
+    * @ejb:interface-method
+    */
+   public void testPreparedStatementReset()
+   {
+      Connection c = null;
+      try
+      {
+         DataSource ds = (DataSource)new InitialContext().lookup("java:StatementTestsConnectionDS");
+         c = ds.getConnection();
+         PreparedStatement ps = c.prepareStatement("dummy");
+         Statement s = getWrappedStatement(ps);
+
+         int maxFieldSize = ps.getMaxFieldSize();
+         int maxRows = ps.getMaxRows();
+         int queryTimeout = ps.getQueryTimeout();
+         int fetchDirection = ps.getFetchDirection();
+         int fetchSize = ps.getFetchSize();
+
+         ps.setMaxFieldSize(1);
+         ps.setMaxRows(2);
+         ps.setQueryTimeout(3);
+         ps.setFetchDirection(ResultSet.FETCH_REVERSE);
+         ps.setFetchSize(4);
+
+         ps.close();
+
+         ps = c.prepareStatement("dummy");
+
+         if (s != getWrappedStatement(ps))
+            throw new EJBException("No ps cache, expected=" + s + " actual = " + getWrappedStatement(ps));
+
+         if (maxFieldSize != ps.getMaxFieldSize())
+            throw new EJBException("maxFieldSize not reset, expected=" + maxFieldSize + " actual=" + ps.getMaxFieldSize());
+         if (maxRows != ps.getMaxRows())
+            throw new EJBException("maxRows not reset, expected=" + maxRows + " actual=" + ps.getMaxRows());
+         if (queryTimeout != ps.getQueryTimeout())
+            throw new EJBException("queryTimeout not reset, expected=" + queryTimeout + " actual=" + ps.getQueryTimeout());
+         if (fetchDirection != ps.getFetchDirection())
+            throw new EJBException("fetchDirection not reset, expected=" + fetchDirection + " actual=" + ps.getFetchDirection());
+         if (fetchSize != ps.getFetchSize())
+            throw new EJBException("fetchSize not reset, expected=" + fetchSize + " actual=" + ps.getFetchSize());
+
+         ps.close();
+
+      }
+      catch (SQLException e)
+      {
+         throw new EJBException(e);
+      } // end of try-catch
+      catch (NamingException e)
+      {
+         throw new EJBException(e);
+      } // end of try-catch
+      finally
+      {
+         try
+         {
+            c.close();
+         }
+         catch (Throwable ignored)
+         {
+         }
+      }
+
+   }
+
+   public Statement getWrappedStatement(Statement s)
+   {
+      while (s instanceof WrappedStatement)
+         s = ((WrappedStatement) s).getUnderlyingStatement();
+      return s;
+   }
+
    public void ejbCreate()
    {
    }

Modified: branches/JBoss_3_2_6_CP/testsuite/src/main/org/jboss/test/jca/jdbc/TestConnection.java
===================================================================
--- branches/JBoss_3_2_6_CP/testsuite/src/main/org/jboss/test/jca/jdbc/TestConnection.java	2007-07-03 21:07:54 UTC (rev 63805)
+++ branches/JBoss_3_2_6_CP/testsuite/src/main/org/jboss/test/jca/jdbc/TestConnection.java	2007-07-03 23:53:31 UTC (rev 63806)
@@ -163,7 +163,7 @@
 
    public PreparedStatement prepareStatement(String sql)
    {
-      return null;
+      return new TestPreparedStatement(driver);
    }
 
    public PreparedStatement prepareStatement(String sql, int rst, int rsc)

Copied: branches/JBoss_3_2_6_CP/testsuite/src/main/org/jboss/test/jca/jdbc/TestPreparedStatement.java (from rev 63804, branches/JBoss_3_2_6_JBAS-2887/testsuite/src/main/org/jboss/test/jca/jdbc/TestPreparedStatement.java)
===================================================================
--- branches/JBoss_3_2_6_CP/testsuite/src/main/org/jboss/test/jca/jdbc/TestPreparedStatement.java	                        (rev 0)
+++ branches/JBoss_3_2_6_CP/testsuite/src/main/org/jboss/test/jca/jdbc/TestPreparedStatement.java	2007-07-03 23:53:31 UTC (rev 63806)
@@ -0,0 +1,193 @@
+/*
+ * JBoss, the OpenSource J2EE webOS
+ *
+ * Distributable under LGPL license.
+ * See terms of license at gnu.org.
+ */
+
+package org.jboss.test.jca.jdbc;
+
+import java.io.InputStream;
+import java.io.Reader;
+import java.math.BigDecimal;
+import java.net.URL;
+import java.sql.Array;
+import java.sql.Blob;
+import java.sql.Clob;
+import java.sql.Date;
+import java.sql.ParameterMetaData;
+import java.sql.PreparedStatement;
+import java.sql.Ref;
+import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
+import java.sql.Time;
+import java.sql.Timestamp;
+import java.util.Calendar;
+
+/**
+ * TestPreparedStatement.java
+ *
+ * @author <a href="mailto:adrian at jboss.com">Adrian Brock</a>
+ */
+
+public class TestPreparedStatement extends TestStatement
+   implements PreparedStatement
+{
+   public TestPreparedStatement(final TestDriver driver)
+   {
+      super(driver);
+   }
+
+   public void addBatch()
+   {
+   }
+
+   public void clearParameters()
+   {
+   }
+
+   public boolean execute()
+   {
+      return true;
+   }
+
+   public ResultSet executeQuery()
+   {
+      return null;
+   }
+
+   public int executeUpdate()
+   {
+      return 0;
+   }
+
+   public ResultSetMetaData getMetaData()
+   {
+      return null;
+   }
+
+   public ParameterMetaData getParameterMetaData()
+   {
+      return null;
+   }
+
+   public void setArray(int i, Array x)
+   {
+   }
+
+   public void setAsciiStream(int i, InputStream is, int length)
+   {
+   }
+
+   public void setBigDecimal(int i, BigDecimal x)
+   {
+   }
+
+   public void setBinaryStream(int i, InputStream is, int length)
+   {
+   }
+
+   public void setBlob(int i, Blob x)
+   {
+   }
+
+   public void setBoolean(int i, boolean x)
+   {
+   }
+
+   public void setByte(int i, byte x)
+   {
+   }
+
+   public void setBytes(int i, byte[] x)
+   {
+   }
+
+   public void setCharacterStream(int i, Reader r, int length)
+   {
+   }
+
+   public void setClob(int i, Clob x)
+   {
+   }
+
+   public void setDate(int i, Date x)
+   {
+   }
+
+   public void setDate(int i, Date x, Calendar cal)
+   {
+   }
+
+   public void setDouble(int i, double x)
+   {
+   }
+
+   public void setFloat(int i, float x)
+   {
+   }
+
+   public void setInt(int i, int x)
+   {
+   }
+
+   public void setLong(int i, long x)
+   {
+   }
+
+   public void setNull(int i, int sqlType)
+   {
+   }
+
+   public void setNull(int i, int sqlType, String typeName)
+   {
+   }
+
+   public void setObject(int i, Object x)
+   {
+   }
+
+   public void setObject(int i, Object x, int targetSqlType)
+   {
+   }
+
+   public void setObject(int i, Object x, int targetSqlType, int scale)
+   {
+   }
+
+   public void setRef(int i, Ref ref)
+   {
+   }
+
+   public void setShort(int i, short x)
+   {
+   }
+
+   public void setString(int i, String x)
+   {
+   }
+
+   public void setTime(int i, Time x)
+   {
+   }
+
+   public void setTime(int i, Time x, Calendar cal)
+   {
+   }
+
+   public void setTimestamp(int i, Timestamp x)
+   {
+   }
+
+   public void setTimestamp(int i, Timestamp x, Calendar cal)
+   {
+   }
+
+   public void setUnicodeStream(int i, InputStream x, int length)
+   {
+   }
+
+   public void setURL(int i, URL url)
+   {
+   }
+}

Modified: branches/JBoss_3_2_6_CP/testsuite/src/main/org/jboss/test/jca/jdbc/TestStatement.java
===================================================================
--- branches/JBoss_3_2_6_CP/testsuite/src/main/org/jboss/test/jca/jdbc/TestStatement.java	2007-07-03 21:07:54 UTC (rev 63805)
+++ branches/JBoss_3_2_6_CP/testsuite/src/main/org/jboss/test/jca/jdbc/TestStatement.java	2007-07-03 23:53:31 UTC (rev 63806)
@@ -30,6 +30,12 @@
 
    private final TestDriver driver;
 
+   int maxFieldSize = 0;
+   int maxRows = 0;
+   int queryTimeout = 0;
+   int fetchDirection = ResultSet.FETCH_FORWARD;
+   int fetchSize = 0;
+
    public TestStatement(final TestDriver driver)
    {
       this.driver = driver;
@@ -191,7 +197,7 @@
     */
    public int getMaxFieldSize() throws SQLException
    {
-      return 0;
+      return maxFieldSize;
    }
 
    /**
@@ -201,6 +207,7 @@
     */
    public void setMaxFieldSize(int max) throws SQLException
    {
+      this.maxFieldSize = max;
    }
 
    /**
@@ -210,7 +217,7 @@
     */
    public int getMaxRows() throws SQLException
    {
-      return 0;
+      return maxRows;
    }
 
    /**
@@ -220,6 +227,7 @@
     */
    public void setMaxRows(int max) throws SQLException
    {
+      this.maxRows = max;
    }
 
    /**
@@ -238,7 +246,7 @@
     */
    public int getQueryTimeout() throws SQLException
    {
-      return 0;
+      return queryTimeout;
    }
 
    /**
@@ -248,6 +256,7 @@
     */
    public void setQueryTimeout(int timeout) throws SQLException
    {
+      this.queryTimeout = timeout;
    }
 
    /**
@@ -315,6 +324,7 @@
     */
    public void setFetchDirection(int direction) throws SQLException
    {
+      this.fetchDirection = direction;
    }
 
    /**
@@ -324,7 +334,7 @@
     */
    public int getFetchDirection() throws SQLException
    {
-      return 0;
+      return fetchDirection;
    }
 
    /**
@@ -334,6 +344,7 @@
     */
    public void setFetchSize(int rows) throws SQLException
    {
+      this.fetchSize = rows;
    }
 
    /**
@@ -343,7 +354,7 @@
     */
    public int getFetchSize() throws SQLException
    {
-      return 0;
+      return fetchSize;
    }
 
    /**

Modified: branches/JBoss_3_2_6_CP/testsuite/src/main/org/jboss/test/jca/test/JDBCStatementTestsConnectionUnitTestCase.java
===================================================================
--- branches/JBoss_3_2_6_CP/testsuite/src/main/org/jboss/test/jca/test/JDBCStatementTestsConnectionUnitTestCase.java	2007-07-03 21:07:54 UTC (rev 63805)
+++ branches/JBoss_3_2_6_CP/testsuite/src/main/org/jboss/test/jca/test/JDBCStatementTestsConnectionUnitTestCase.java	2007-07-03 23:53:31 UTC (rev 63806)
@@ -44,4 +44,18 @@
       s.testConnectionObtainable();
    }
 
+   /** This test will probably fail with a class cast exception if run
+    * twice. The DriverManager appears to be keeping a static
+    * reference to the Driver class, so reloading the
+    * jbosstestdriver.sar will result in incompatible classes being
+    * used.
+    */
+   public void testJDBCPreparedStatementReset() throws Exception
+   {
+      JDBCStatementTestsConnectionSessionHome home =
+         (JDBCStatementTestsConnectionSessionHome)getInitialContext().lookup("JDBCStatementTestsConnectionSession");
+      JDBCStatementTestsConnectionSession s = home.create();
+      s.testPreparedStatementReset();
+   }
+
 }// JDBCStatementTestsConnectionUnitTestCase

Modified: branches/JBoss_3_2_6_CP/testsuite/src/resources/jca/jdbc/testdriver-ds.xml
===================================================================
--- branches/JBoss_3_2_6_CP/testsuite/src/resources/jca/jdbc/testdriver-ds.xml	2007-07-03 21:07:54 UTC (rev 63805)
+++ branches/JBoss_3_2_6_CP/testsuite/src/resources/jca/jdbc/testdriver-ds.xml	2007-07-03 23:53:31 UTC (rev 63806)
@@ -20,6 +20,7 @@
     <max-pool-size>1</max-pool-size>
     <blocking-timeout-millis>5000</blocking-timeout-millis>
     <idle-timeout-minutes>15</idle-timeout-minutes>
+    <prepared-statement-cache-size>100</prepared-statement-cache-size>
 
   </no-tx-datasource>
 </datasources>




More information about the jboss-cvs-commits mailing list