Author: kahatlen
Date: Fri Mar 10 02:17:11 2006
New Revision: 384753
URL: http://svn.apache.org/viewcvs?rev=384753&view=rev
Log:
DERBY-947: Miscellaneous PreparedStatement methods added by JDBC4
As described in the JDBC 4 spec sections 13.2 and 3.1.
This involves building support for JDBC4 methods added to
PreparedStatement and not addressed by other JIRAs: isPoolable() and
setPoolable().
Patch contributed by Francois Orsini.
Modified:
db/derby/code/trunk/java/client/org/apache/derby/client/am/PreparedStatement40.java
db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedPreparedStatement40.java
db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestPreparedStatementMethods.java
Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/PreparedStatement40.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/client/org/apache/derby/client/am/PreparedStatement40.java?rev=384753&r1=384752&r2=384753&view=diff
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/PreparedStatement40.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/PreparedStatement40.java Fri
Mar 10 02:17:11 2006
@@ -29,6 +29,9 @@
public class PreparedStatement40 extends org.apache.derby.client.am.PreparedStatement{
+ // By default a PreparedStatement is poolable when it is created
+ private boolean isPoolable = true;
+
public PreparedStatement40(Agent agent,
Connection connection,
String sql,
@@ -133,13 +136,59 @@
throw SQLExceptionFactory.notImplemented ("setSQLXML (int, SQLXML)");
}
+ /**
+ * Requests that a PreparedStatement be pooled or not.
+ *
+ * @param poolable requests that the statement be pooled if true and that the
+ * statement not be pooled if false
+ * @throws SQLException if the PreparedStatement has been closed.
+ */
+
public void setPoolable(boolean poolable)
- throws SQLException{
- throw SQLExceptionFactory.notImplemented ("setPoolable (boolean)");
+ throws SQLException {
+ try
+ {
+ synchronized (connection_) {
+ if (agent_.loggingEnabled()) {
+ agent_.logWriter_.traceEntry(this, "setPoolable", poolable);
+ }
+ // Assert the statement has not been closed
+ checkForClosedStatement();
+
+ isPoolable = poolable;
+ }
+ }
+ catch (SqlException se)
+ {
+ throw se.getSQLException();
+ }
}
+ /**
+ * Returns the value of the statements poolable hint, indicating whether
+ * pooling of the statement is requested.
+ *
+ * @return The value of the statement's poolable hint.
+ * @throws SQLException if the PreparedStatement has been closed.
+ */
+
public boolean isPoolable()
throws SQLException{
- throw SQLExceptionFactory.notImplemented ("isPoolable ()");
+ try
+ {
+ synchronized (connection_) {
+ if (agent_.loggingEnabled()) {
+ agent_.logWriter_.traceEntry(this, "isPoolable");
+ }
+ // Assert the statement has not been closed
+ checkForClosedStatement();
+
+ return isPoolable;
+ }
+ }
+ catch (SqlException se)
+ {
+ throw se.getSQLException();
+ }
}
}
Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedPreparedStatement40.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedPreparedStatement40.java?rev=384753&r1=384752&r2=384753&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedPreparedStatement40.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedPreparedStatement40.java
Fri Mar 10 02:17:11 2006
@@ -30,7 +30,10 @@
import org.apache.derby.iapi.reference.SQLState;
import org.apache.derby.iapi.error.StandardException;
-public class EmbedPreparedStatement40 extends EmbedPreparedStatement30{
+public class EmbedPreparedStatement40 extends EmbedPreparedStatement30 {
+
+ // By default a PreparedStatement is poolable when it is created
+ private boolean isPoolable = true;
public EmbedPreparedStatement40(EmbedConnection conn, String sql, boolean forMetaData,
int resultSetType, int resultSetConcurrency, int resultSetHoldability,
@@ -116,13 +119,35 @@
throw Util.notImplemented();
}
+ /**
+ * Requests that a PreparedStatement be pooled or not.
+ *
+ * @param poolable requests that the statement be pooled if true and that the
+ * statement not be pooled if false
+ * @throws SQLException if the PreparedStatement has been closed.
+ */
+
public void setPoolable(boolean poolable)
- throws SQLException{
- throw Util.notImplemented();
+ throws SQLException {
+ // Assert the statement is still active (not closed)
+ checkStatus();
+
+ isPoolable = poolable;
}
+ /**
+ * Returns the value of the statements poolable hint, indicating whether
+ * pooling of the statement is requested.
+ *
+ * @return The value of the statement's poolable hint.
+ * @throws SQLException if the PreparedStatement has been closed.
+ */
+
public boolean isPoolable()
- throws SQLException{
- throw Util.notImplemented();
+ throws SQLException {
+ // Assert the statement is still active (not closed)
+ checkStatus();
+
+ return isPoolable;
}
}
Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestPreparedStatementMethods.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestPreparedStatementMethods.java?rev=384753&r1=384752&r2=384753&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestPreparedStatementMethods.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestPreparedStatementMethods.java
Fri Mar 10 02:17:11 2006
@@ -36,6 +36,7 @@
import java.sql.ResultSet;
import java.sql.SQLXML;
import java.sql.Statement;
+import org.apache.derby.shared.common.error.ExceptionUtil;
import org.apache.derby.shared.common.reference.SQLState;
import org.apache.derby.tools.ij;
/**
@@ -45,8 +46,9 @@
public class TestPreparedStatementMethods {
- static Connection conn=null;
- PreparedStatement ps=null;
+ static Connection conn = null;
+ PreparedStatement ps = null;
+ boolean stmtIsClosed = false;
String filepath;
String sep;
@@ -427,30 +429,54 @@
}
void t_setPoolable() {
try {
+ // Set the poolable statement hint to false
ps.setPoolable(false);
- System.out.println("UnImplemented Exception not thrown in code");
- } catch(SQLException e) {
- if(SQLState.NOT_IMPLEMENTED.equals (e.getSQLState())) {
- System.out.println("Unexpected SQLException"+e);
+ if (ps.isPoolable())
+ System.out.println("Expected a non-poolable statement");
+ // Set the poolable statement hint to true
+ ps.setPoolable(true);
+ if (!ps.isPoolable())
+ System.out.println("Expected a poolable statement");
+ } catch(SQLException sqle) {
+ // Check which SQLException state we've got and if it is
+ // expected, do not print a stackTrace
+ // FIXME: Note that the test for stmtIsClosed boolean can
+ // be removed _once_ the client driver reports the same
+ // SQLState as the embedded driver does - See JIRA-254.
+ if (ExceptionUtil.getSQLStateFromIdentifier(
+ SQLState.ALREADY_CLOSED).equals(sqle.getSQLState()) ||
+ stmtIsClosed) {
+ // All is good and is expected
+ } else {
+ System.out.println("Unexpected SQLException " + sqle);
+ sqle.printStackTrace();
}
-
} catch(Exception e) {
- System.out.println("Unexpected exception thrown in method"+e);
+ System.out.println("Unexpected exception thrown in method " + e);
e.printStackTrace();
}
}
void t_isPoolable() {
try {
- boolean b;
- b = ps.isPoolable();
- System.out.println("UnImplemented Exception not thrown in code");
- } catch(SQLException e) {
- if(SQLState.NOT_IMPLEMENTED.equals (e.getSQLState())) {
- System.out.println("Unexpected SQLException"+e);
+ // By default a prepared statement is poolable
+ if (!ps.isPoolable())
+ System.out.println("Expected a poolable statement");
+ } catch(SQLException sqle) {
+ // Check which SQLException state we've got and if it is
+ // expected, do not print a stackTrace
+ // FIXME: Note that the test for stmtIsClosed boolean can
+ // be removed _once_ the client driver reports the same
+ // SQLState as the embedded driver does - See JIRA-254.
+ if (ExceptionUtil.getSQLStateFromIdentifier(
+ SQLState.ALREADY_CLOSED).equals(sqle.getSQLState()) ||
+ stmtIsClosed) {
+ // All is good and is expected
+ } else {
+ System.out.println("Unexpected SQLException " + sqle);
+ sqle.printStackTrace();
}
-
} catch(Exception e) {
- System.out.println("Unexpected exception thrown in method"+e);
+ System.out.println("Unexpected exception thrown in method " + e);
e.printStackTrace();
}
}
@@ -458,8 +484,9 @@
* Start the tests for the JDBC4.0 methods on the client side
*/
void startClientTestMethods() {
- Connection conn_main=null;
- PreparedStatement ps_main=null;
+ Connection conn_main = null;
+ PreparedStatement ps_main = null;
+ stmtIsClosed = false;
try {
Class.forName("org.apache.derby.jdbc.ClientDriver");
@@ -478,8 +505,16 @@
t_setBlob();
t_setNClob2();
t_setSQLXML();
+ t_isPoolable();
t_setPoolable();
+ // Close the prepared statement and verify the poolable hint
+ // cannot be set or retrieved
+ ps.close();
+ stmtIsClosed = true; // This until JIRA-953 is addressed then
+ // stmt.isClosed() can be called in the
+ // poolable hint set/get test methods below
t_isPoolable();
+ t_setPoolable();
} catch(SQLException sqle) {
sqle.printStackTrace();
} catch(ClassNotFoundException cnfe) {
@@ -496,8 +531,9 @@
* Start the tests for testing the JDBC4.0 methods on the embedded side
*/
void startEmbeddedTestMethods() {
- Connection conn_main=null;
- PreparedStatement ps_main=null;
+ Connection conn_main = null;
+ PreparedStatement ps_main = null;
+ stmtIsClosed = false;
try {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
@@ -509,12 +545,13 @@
File file = new File("extin/short.txt");
int fileLength = (int) file.length();
InputStream fin = new FileInputStream(file);
- PreparedStatement ps = conn_main.prepareStatement("INSERT INTO " +
+ ps = conn_main.prepareStatement("INSERT INTO " +
"clobtable3 " +
"VALUES (?, ?)");
ps.setInt(1, 1000);
ps.setAsciiStream(2, fin, fileLength);
ps.execute();
+ ps.close();
Statement s1 = conn_main.createStatement();
s1.execute("create table blobtable3 (n int,blobcol BLOB)");
@@ -541,8 +578,16 @@
t_setNClob1();
t_setNClob2();
t_setSQLXML();
+ t_isPoolable();
t_setPoolable();
+ // Close the prepared statement and verify the poolable hint
+ // cannot be set or retrieved
+ ps.close();
+ stmtIsClosed = true; // This until JIRA-953 is addressed then
+ // stmt.isClosed() can be called in the
+ // poolable hint set/get test methods below
t_isPoolable();
+ t_setPoolable();
}
catch(SQLException sqle) {
sqle.printStackTrace();
|