You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by rh...@apache.org on 2006/04/03 19:39:21 UTC
svn commit: r391090 - in
/db/derby/code/trunk/java/engine/org/apache/derby/jdbc:
EmbedPooledConnection40.java EmbedXAConnection40.java XAStatementControl.java
Author: rhillegas
Date: Mon Apr 3 10:39:19 2006
New Revision: 391090
URL: http://svn.apache.org/viewcvs?rev=391090&view=rev
Log:
Commit Anurag's patch to DERBY-1163, adding JDBC4-specific versions of embedded pooled connection and embedded xa connection.
Added:
db/derby/code/trunk/java/engine/org/apache/derby/jdbc/EmbedPooledConnection40.java (with props)
db/derby/code/trunk/java/engine/org/apache/derby/jdbc/EmbedXAConnection40.java (with props)
Modified:
db/derby/code/trunk/java/engine/org/apache/derby/jdbc/XAStatementControl.java
Added: db/derby/code/trunk/java/engine/org/apache/derby/jdbc/EmbedPooledConnection40.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/engine/org/apache/derby/jdbc/EmbedPooledConnection40.java?rev=391090&view=auto
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/jdbc/EmbedPooledConnection40.java (added)
+++ db/derby/code/trunk/java/engine/org/apache/derby/jdbc/EmbedPooledConnection40.java Mon Apr 3 10:39:19 2006
@@ -0,0 +1,74 @@
+/*
+
+ Derby - Class org.apache.derby.jdbc.EmbedPooledConnection40
+
+ Copyright 2001, 2005 The Apache Software Foundation or its licensors,
+ as applicable.
+
+ Licensed 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.derby.jdbc;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import javax.sql.StatementEventListener;
+
+/**
+ * This class supports jdbc4.0 javax.sql.PooledConnection
+ * older methods are inherited from EmbedPooledConnection
+ */
+class EmbedPooledConnection40 extends EmbedPooledConnection {
+
+ EmbedPooledConnection40 (ReferenceableDataSource ds, String user,
+ String password, boolean requestPassword) throws SQLException {
+ super (ds, user, password, requestPassword);
+ }
+ /**
+ * Removes the specified <code>StatementEventListener</code> from the list of
+ * components that will be notified when the driver detects that a
+ * <code>PreparedStatement</code> has been closed or is invalid.
+ * <p>
+ *
+ * @param listener the component which implements the
+ * <code>StatementEventListener</code> interface that was previously
+ * registered with this <code>PooledConnection</code> object
+ * <p>
+ * @since 1.6
+ */
+ public void removeStatementEventListener(StatementEventListener listener) {
+ throw new UnsupportedOperationException (
+ "addStatementEventListener(StatementEventListener listener)");
+ }
+
+ /**
+ * Registers a <code>StatementEventListener</code> with this
+ * <code>PooledConnection</code> object. Components that
+ * wish to be notified when <code>PreparedStatement</code>s created by the
+ * connection are closed or are detected to be invalid may use this method
+ * to register a <code>StatementEventListener</code> with this
+ * <code>PooledConnection</code> object.
+ * <p>
+ *
+ * @param listener an component which implements the
+ * <code>StatementEventListener</code> interface that is to be registered
+ * with this <code>PooledConnection</code> object
+ * <p>
+ * @since 1.6
+ */
+ public void addStatementEventListener(StatementEventListener listener) {
+ throw new UnsupportedOperationException (
+ "addStatementEventListener(StatementEventListener listener)");
+ }
+}
Propchange: db/derby/code/trunk/java/engine/org/apache/derby/jdbc/EmbedPooledConnection40.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: db/derby/code/trunk/java/engine/org/apache/derby/jdbc/EmbedXAConnection40.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/engine/org/apache/derby/jdbc/EmbedXAConnection40.java?rev=391090&view=auto
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/jdbc/EmbedXAConnection40.java (added)
+++ db/derby/code/trunk/java/engine/org/apache/derby/jdbc/EmbedXAConnection40.java Mon Apr 3 10:39:19 2006
@@ -0,0 +1,240 @@
+/*
+
+ Derby - class org.apache.derby.jdbc.EmbedXAConnection40
+
+ Copyright 2006 The Apache Software Foundation or its licensors, as applicable.
+
+ Licensed 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.derby.jdbc;
+
+import java.sql.CallableStatement;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.sql.Statement;
+import javax.sql.XAConnection;
+import javax.transaction.xa.XAResource;
+import org.apache.derby.iapi.jdbc.EngineConnection;
+import org.apache.derby.iapi.jdbc.ResourceAdapter;
+import org.apache.derby.iapi.reference.JDBC30Translation;
+import org.apache.derby.iapi.reference.SQLState;
+import org.apache.derby.impl.jdbc.Util;
+
+/**
+ * This class implements jdbc4.0 methods of XAConnection
+ * It inherits these methods from EmbedPooledConnection40
+ */
+final class EmbedXAConnection40 extends EmbedPooledConnection40
+ implements XAConnection {
+ private EmbedXAResource xaRes;
+
+ /**
+ * Creates a new instance of EmbedXAConnection40. Initializes XAResource for
+ * the connection
+ * @param ds
+ * @param ra
+ * @param user
+ * @param password
+ * @param requestPassword
+ */
+ EmbedXAConnection40(EmbeddedDataSource40 ds, ResourceAdapter ra,
+ String user, String password,
+ boolean requestPassword) throws SQLException {
+ super(ds, user, password, requestPassword);
+ xaRes = new EmbedXAResource(this, ra);
+ }
+
+ /**
+ * Retrieves an <code>XAResource</code> object that
+ * the transaction manager will use
+ * to manage this <code>XAConnection</code> object's participation in a
+ * distributed transaction.
+ *
+ * @return the <code>XAResource</code> object
+ * @exception SQLException if a database access error occurs
+ */
+ public final synchronized XAResource getXAResource() throws SQLException {
+ checkActive ();
+ return xaRes;
+ }
+
+ /**
+ * Allow control over setting auto commit mode.
+ * @param autoCommit
+ */
+ public void checkAutoCommit(boolean autoCommit) throws SQLException {
+ if (autoCommit && (xaRes.getCurrentXid() != null))
+ throw Util.generateCsSQLException(SQLState.CANNOT_AUTOCOMMIT_XA);
+
+ super.checkAutoCommit(autoCommit);
+ }
+
+ /**
+ * Are held cursors allowed. If the connection is attached to
+ * a global transaction then downgrade the result set holdabilty
+ * to CLOSE_CURSORS_AT_COMMIT if downgrade is true, otherwise
+ * throw an exception.
+ * If the connection is in a local transaction then the
+ * passed in holdabilty is returned.
+ * @param holdability
+ * @param downgrade
+ * @return holdability
+ */
+ public int checkHoldCursors(int holdability, boolean downgrade)
+ throws SQLException {
+ if (holdability == JDBC30Translation.HOLD_CURSORS_OVER_COMMIT) {
+ if (xaRes.getCurrentXid() != null) {
+ if (downgrade)
+ return JDBC30Translation.CLOSE_CURSORS_AT_COMMIT;
+ throw Util.generateCsSQLException(SQLState.CANNOT_HOLD_CURSOR_XA);
+ }
+ }
+
+ return super.checkHoldCursors(holdability, downgrade);
+ }
+
+ /**
+ * Allow control over creating a Savepoint (JDBC 3.0)
+ */
+ public void checkSavepoint() throws SQLException {
+
+ if (xaRes.getCurrentXid() != null)
+ throw Util.generateCsSQLException(SQLState.CANNOT_ROLLBACK_XA);
+
+ super.checkSavepoint();
+ }
+
+ /**
+ * Allow control over calling rollback.
+ */
+ public void checkRollback() throws SQLException {
+
+ if (xaRes.getCurrentXid() != null)
+ throw Util.generateCsSQLException(SQLState.CANNOT_ROLLBACK_XA);
+
+ super.checkRollback();
+ }
+ /**
+ * Allow control over calling commit.
+ */
+ public void checkCommit() throws SQLException {
+
+ if (xaRes.getCurrentXid() != null)
+ throw Util.generateCsSQLException(SQLState.CANNOT_COMMIT_XA);
+
+ super.checkCommit();
+ }
+
+ /**
+ * Create an object handle for a database connection.
+ * @return a Connection object
+ * @exception SQLException - if a database-access error occurs.
+ */
+ public Connection getConnection() throws SQLException {
+ Connection handle;
+
+ // Is this just a local transaction?
+ if (xaRes.getCurrentXid() == null) {
+ handle = super.getConnection();
+ } else {
+
+ if (currentConnectionHandle != null) {
+ // this can only happen if someone called start(Xid),
+ // getConnection, getConnection (and we are now the 2nd
+ // getConnection call).
+ // Cannot yank a global connection away like, I don't think...
+ throw Util.generateCsSQLException(
+ SQLState.CANNOT_CLOSE_ACTIVE_XA_CONNECTION);
+ }
+
+ handle = getNewCurrentConnectionHandle();
+ }
+
+ currentConnectionHandle.syncState();
+
+ return handle;
+ }
+
+ /**
+ * Wrap and control a Statement
+ * @param s
+ * @return Statement
+ */
+ public Statement wrapStatement(Statement s) throws SQLException {
+ XAStatementControl sc = new XAStatementControl(this, s);
+ return sc.applicationStatement;
+ }
+
+ /**
+ * Wrap and control a PreparedStatement
+ * @param ps
+ * @param sql
+ * @param generatedKeys
+ * @return PreparedStatement
+ */
+ public PreparedStatement wrapStatement(PreparedStatement ps, String sql,
+ Object generatedKeys) throws SQLException {
+ XAStatementControl sc = new XAStatementControl(this, ps,
+ sql, generatedKeys);
+ return (PreparedStatement) sc.applicationStatement;
+ }
+
+ /**
+ * Wrap and control a PreparedStatement
+ * @param cs
+ * @param sql
+ * @return CallableStatement
+ */
+ public CallableStatement wrapStatement(CallableStatement cs,
+ String sql) throws SQLException {
+ XAStatementControl sc = new XAStatementControl(this, cs, sql);
+ return (CallableStatement) sc.applicationStatement;
+ }
+
+ /**
+ * Override getRealConnection to create a a local connection
+ * when we are not associated with an XA transaction.
+ *
+ * This can occur if the application has a Connection object (conn)
+ * and the following sequence occurs.
+ *
+ * conn = xac.getConnection();
+ * xac.start(xid, ...)
+ *
+ * // do work with conn
+ *
+ * xac.end(xid, ...);
+ *
+ * // do local work with conn
+ * // need to create new connection here.
+ */
+ public EngineConnection getRealConnection() throws SQLException {
+ EngineConnection rc = super.getRealConnection();
+ if (rc != null)
+ return rc;
+
+ openRealConnection();
+
+ // a new Connection, set its state according to the application's
+ //Connection handle
+ currentConnectionHandle.setState(true);
+ return realConnection;
+ }
+
+
+
+
+}
Propchange: db/derby/code/trunk/java/engine/org/apache/derby/jdbc/EmbedXAConnection40.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: db/derby/code/trunk/java/engine/org/apache/derby/jdbc/XAStatementControl.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/engine/org/apache/derby/jdbc/XAStatementControl.java?rev=391090&r1=391089&r2=391090&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/jdbc/XAStatementControl.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/jdbc/XAStatementControl.java Mon Apr 3 10:39:19 2006
@@ -41,7 +41,7 @@
/**
*/
- private final EmbedXAConnection xaConnection;
+ private final EmbedPooledConnection xaConnection;
private final BrokeredConnection applicationConnection;
BrokeredStatement applicationStatement;
private EmbedConnection realConnection;
@@ -49,23 +49,28 @@
private PreparedStatement realPreparedStatement;
private CallableStatement realCallableStatement;
- private XAStatementControl(EmbedXAConnection xaConnection) {
+ private XAStatementControl(EmbedPooledConnection xaConnection) {
this.xaConnection = xaConnection;
this.realConnection = xaConnection.realConnection;
this.applicationConnection = xaConnection.currentConnectionHandle;
}
- XAStatementControl(EmbedXAConnection xaConnection, Statement realStatement) throws SQLException {
+ XAStatementControl(EmbedPooledConnection xaConnection,
+ Statement realStatement) throws SQLException {
this(xaConnection);
this.realStatement = realStatement;
this.applicationStatement = applicationConnection.newBrokeredStatement(this);
}
- XAStatementControl(EmbedXAConnection xaConnection, PreparedStatement realPreparedStatement, String sql, Object generatedKeys) throws SQLException {
+ XAStatementControl(EmbedPooledConnection xaConnection,
+ PreparedStatement realPreparedStatement,
+ String sql, Object generatedKeys) throws SQLException {
this(xaConnection);
this.realPreparedStatement = realPreparedStatement;
this.applicationStatement = applicationConnection.newBrokeredStatement(this, sql, generatedKeys);
}
- XAStatementControl(EmbedXAConnection xaConnection, CallableStatement realCallableStatement, String sql) throws SQLException {
+ XAStatementControl(EmbedPooledConnection xaConnection,
+ CallableStatement realCallableStatement,
+ String sql) throws SQLException {
this(xaConnection);
this.realCallableStatement = realCallableStatement;
this.applicationStatement = applicationConnection.newBrokeredStatement(this, sql);