1 21 22 package org.apache.derby.jdbc; 23 24 import org.apache.derby.impl.jdbc.Util; 25 import org.apache.derby.iapi.jdbc.EngineConnection; 26 import org.apache.derby.iapi.jdbc.ResourceAdapter; 27 28 import org.apache.derby.iapi.reference.SQLState; 29 import org.apache.derby.iapi.reference.JDBC30Translation; 30 31 import java.sql.Connection ; 32 import java.sql.SQLException ; 33 import java.sql.Statement ; 34 import java.sql.PreparedStatement ; 35 import java.sql.CallableStatement ; 36 import javax.transaction.xa.XAResource ; 37 38 39 import javax.sql.XAConnection ; 40 41 43 class EmbedXAConnection extends EmbedPooledConnection 44 implements XAConnection 45 46 { 47 48 private EmbedXAResource xaRes; 49 50 EmbedXAConnection(EmbeddedDataSource ds, ResourceAdapter ra, String u, String p, boolean requestPassword) throws SQLException 51 { 52 super(ds, u, p, requestPassword); 53 xaRes = new EmbedXAResource (this, ra); 54 } 55 56 59 60 public final synchronized XAResource getXAResource() throws SQLException { 61 checkActive(); 62 return xaRes; 63 } 64 65 68 71 public void checkAutoCommit(boolean autoCommit) throws SQLException { 72 if (autoCommit && (xaRes.getCurrentXid () != null)) 73 throw Util.generateCsSQLException(SQLState.CANNOT_AUTOCOMMIT_XA); 74 75 super.checkAutoCommit(autoCommit); 76 } 77 85 public int checkHoldCursors(int holdability, boolean downgrade) 86 throws SQLException 87 { 88 if (holdability == JDBC30Translation.HOLD_CURSORS_OVER_COMMIT) { 89 if (xaRes.getCurrentXid () != null) { 90 if (!downgrade) 91 throw Util.generateCsSQLException(SQLState.CANNOT_HOLD_CURSOR_XA); 92 93 holdability = JDBC30Translation.CLOSE_CURSORS_AT_COMMIT; 94 } 95 } 96 97 return super.checkHoldCursors(holdability, downgrade); 98 } 99 100 103 public void checkSavepoint() throws SQLException { 104 105 if (xaRes.getCurrentXid () != null) 106 throw Util.generateCsSQLException(SQLState.CANNOT_ROLLBACK_XA); 107 108 super.checkSavepoint(); 109 } 110 111 114 public void checkRollback() throws SQLException { 115 116 if (xaRes.getCurrentXid () != null) 117 throw Util.generateCsSQLException(SQLState.CANNOT_ROLLBACK_XA); 118 119 super.checkRollback(); 120 } 121 124 public void checkCommit() throws SQLException { 125 126 if (xaRes.getCurrentXid () != null) 127 throw Util.generateCsSQLException(SQLState.CANNOT_COMMIT_XA); 128 129 super.checkCommit(); 130 } 131 132 public Connection getConnection() throws SQLException 133 { 134 Connection handle; 135 136 if (xaRes.getCurrentXid () == null) { 138 handle = super.getConnection(); 139 } else { 140 141 if (currentConnectionHandle != null) { 142 throw Util.generateCsSQLException( 147 SQLState.CANNOT_CLOSE_ACTIVE_XA_CONNECTION); 148 } 149 150 handle = getNewCurrentConnectionHandle(); 151 } 152 153 currentConnectionHandle.syncState(); 154 155 return handle; 156 } 157 158 161 public Statement wrapStatement(Statement s) throws SQLException { 162 XAStatementControl sc = new XAStatementControl(this, s); 163 return sc.applicationStatement; 164 } 165 168 public PreparedStatement wrapStatement(PreparedStatement ps, String sql, Object generatedKeys) throws SQLException { 169 ps = super.wrapStatement(ps,sql,generatedKeys); 170 XAStatementControl sc = new XAStatementControl(this, ps, sql, generatedKeys); 171 return (PreparedStatement ) sc.applicationStatement; 172 } 173 176 public CallableStatement wrapStatement(CallableStatement cs, String sql) throws SQLException { 177 cs = super.wrapStatement(cs,sql); 178 XAStatementControl sc = new XAStatementControl(this, cs, sql); 179 return (CallableStatement ) sc.applicationStatement; 180 } 181 182 199 public EngineConnection getRealConnection() throws SQLException 200 { 201 EngineConnection rc = super.getRealConnection(); 202 if (rc != null) 203 return rc; 204 205 openRealConnection(); 206 207 currentConnectionHandle.setState(true); 209 210 return realConnection; 211 } 212 } 213 | Popular Tags |