package org.enhydra.jdbc.standard;
import java.sql.SQLException;
import java.sql.CallableStatement;
import org.enhydra.jdbc.core.CoreCallableStatement;
import javax.transaction.Transaction;
import javax.transaction.SystemException;
import javax.transaction.RollbackException;
public class StandardXACallableStatement extends CoreCallableStatement {
private StandardXAConnectionHandle con;
private boolean closed;
private String sql;
private int resultSetType;
private int resultSetConcurrency;
private int resultSetHoldability;
StandardXACallableStatement(
StandardXAConnectionHandle con,
String sql,
int resultSetType,
int resultSetConcurrency,
int resultSetHoldability)
throws SQLException {
this.con = con;
this.sql = sql;
this.resultSetType = resultSetType;
this.resultSetConcurrency = resultSetConcurrency;
this.resultSetHoldability = resultSetHoldability;
log = con.log;
}
private CallableStatement newStatement() throws SQLException {
if (resultSetType == 0 && resultSetConcurrency == 0 && resultSetHoldability == 0) {
return con.con.prepareCall(sql);
} else if (resultSetHoldability == 0) {
return con.con.prepareCall(
sql,
resultSetType,
resultSetConcurrency);
} else return con.con.prepareCall(
sql,
resultSetType,
resultSetConcurrency,
resultSetHoldability);
}
public synchronized void close() throws SQLException {
super.close();
closed = true;
}
public synchronized void preInvoke() throws SQLException {
if (closed)
throw new SQLException("Prepare Statement is closed");
Transaction ntx = null;
if (con.tx == null) {
try {
try {
ntx =
(con.transactionManager != null)
? con.transactionManager.getTransaction()
: null;
if (ntx != null) {
con.tx = ntx;
con.xacon.thisAutoCommit = con.getAutoCommit();
con.setAutoCommit(false);
try {
con.tx.enlistResource(con.xacon.getXAResource());
if (cs != null) {
cs.close();
cs = null;
}
} catch (RollbackException n) {
throw new SQLException(
"StandardXAStatement:preInvoke enlistResource exception: "
+ n.toString());
}
}
} catch (SystemException n) {
throw new SQLException(
"StandardXAStatement:preInvoke getTransaction exception: "
+ n.toString());
}
} catch (NullPointerException n) {
throw new SQLException(
"StandardXAStatement:preInvoke should not be used outside an EJBServer: "
+ n.toString());
}
}
if (cs == null) {
cs = newStatement();
}
}
public void catchInvoke(SQLException sqlException) throws SQLException {
throw (sqlException);
}
}