[cvs]
expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/db
DBConnection.java
JCorporate Ltd
jcorp at jcorp2.servlets.net
Mon Sep 20 13:18:58 PDT 2004
Update of /home/javacorp/.cvs/expresso/expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/db
In directory jcorp2.servlets.net:/tmp/cvs-serv6016
Modified Files:
DBConnection.java
Log Message:
<Aucun commentaire entré>
Index: DBConnection.java
===================================================================
RCS file: /home/javacorp/.cvs/expresso/expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/db/DBConnection.java,v
retrieving revision 1.50
retrieving revision 1.51
diff -C2 -d -r1.50 -r1.51
*** DBConnection.java 20 Sep 2004 19:21:48 -0000 1.50
--- DBConnection.java 20 Sep 2004 20:18:55 -0000 1.51
***************
*** 75,78 ****
--- 75,79 ----
import java.io.PrintWriter;
+ import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
***************
*** 99,102 ****
--- 100,104 ----
* pooling, and have special methods to support this.
*
+ * @version $Revision$ $Date$
* @author Michael Nash
*/
***************
*** 146,149 ****
--- 148,156 ----
private PreparedStatement preparedStatement = null;
+ /**
+ * Any callable statements that might be run
+ */
+ private CallableStatement callableStatement = null;
+
/** Current result set */
private ResultSet myResultSet;
***************
*** 288,291 ****
--- 295,376 ----
private int limitationPosition = LIMITATION_DISABLED;
+
+ /** Committed read transaction mode
+ *
+ * author Yves Henri AMAIZO, Mon Dec 22 10:30:59 2003
+ * @since Expresso 5.3
+ *
+ */
+ public static final int TRANSACTION_NORMAL_MODE = 1;
+
+ /** Uncommitted read transaction mode
+ *
+ * author Yves Henri AMAIZO, Mon Dec 22 10:30:59 2003
+ * @since Expresso 5.3
+ *
+ */
+ public static final int TRANSACTION_DIRTY_MODE = 2;
+
+ /** Repeatable read transaction mode
+ *
+ * author Yves Henri AMAIZO, Mon Dec 22 10:30:59 2003
+ * @since Expresso 5.3
+ *
+ */
+ public static final int TRANSACTION_RESTRICTIVE_MODE = 3;
+
+ /** Serializable read transaction mode
+ *
+ * author Yves Henri AMAIZO, Mon Dec 22 10:30:59 2003
+ * @since Expresso 5.3
+ *
+ */
+ public static final int TRANSACTION_EXCLUSIVE_MODE = 4;
+
+
+ /**
+ * Transaction isolation mode set : Default Expresso transaction mode.
+ *
+ * author Yves Henri AMAIZO, Mon Dec 22 10:30:59 2003
+ * @since Expresso 5.3
+ *
+ * @see Connection#TRANSACTION_READ_COMMITTED MODE
+ *
+ */
+ private int transactionCommittedMode = TRANSACTION_NORMAL_MODE;
+
+ /**
+ * Transaction isolation mode set
+ *
+ * author Yves Henri AMAIZO, Mon Dec 22 10:30:59 2003
+ * @since Expresso 5.3
+ *
+ * @see Connection#TRANSACTION_READ_UNCOMMITTED MODE
+ *
+ */
+ private int transactionUncommittedMode = TRANSACTION_DIRTY_MODE;
+
+ /**
+ * Transaction isolation mode set
+ *
+ * author Yves Henri AMAIZO, Mon Dec 22 10:30:59 2003
+ * @since Expresso 5.3
+ *
+ * @see Connection#TRANSACTION_REPEATABLE_READ MODE
+ *
+ */
+ private int transactionRepeatableMode = TRANSACTION_RESTRICTIVE_MODE;
+
+ /**
+ * Transaction isolation mode set
+ *
+ * author Yves Henri AMAIZO, Mon Dec 22 10:30:59 2003
+ * @since Expresso 5.3
+ *
+ * @see Connection#TRANSACTION_SERIALIZABLE MODE
+ *
+ */
+ private int transactionSerializableMode = TRANSACTION_EXCLUSIVE_MODE;
+
/**
* Rowset Limitation Optimisation Syntax String.
***************
*** 759,762 ****
--- 844,891 ----
}
+ /**
+ * Creates a <code>CallableStatment</code> object.
+ * @param sqlString The string to create the callable statement with.
+ * @return an instantiated callable statement, that you then modify for
+ * your various parameters May return null if an exception occurred
+ */
+ public CallableStatement createCallableStatement(String sqlString) {
+ try {
+ if (myConnection == null) {
+ return null;
+ }
+ touch();
+
+ if (callableStatement != null) {
+ try {
+ callableStatement.close();
+ } catch (SQLException ex) {
+ log.warn("Error closing older callable statement: ", ex);
+ }
+ }
+ callableStatement = myConnection.prepareCall(sqlString);
+ } catch (SQLException e) {
+ log.error(e);
+ }
+ return callableStatement;
+ }
+
+
+ /**
+ * Clear a callable statement associated with this record.
+ */
+ public void clearCallableStatement() {
+ if (this.callableStatement != null) {
+ try {
+ callableStatement.close();
+ } catch (SQLException ex) {
+ log.warn("Error clearing callable statement", ex);
+ }
+
+ callableStatement = null;
+ }
+ }
+
+
/**
***************
*** 779,783 ****
try {
if (myConnection.getAutoCommit() == false) {
! myConnection.commit();
}
}
--- 908,913 ----
try {
if (myConnection.getAutoCommit() == false) {
! // myConnection.commit();
! myConnection.rollback();
}
}
***************
*** 801,804 ****
--- 931,935 ----
myConnection = null;
isConnected = false;
+ immortal = false;
} /* disconnect() */
***************
*** 1024,1027 ****
--- 1155,1251 ----
+ /**
+ * Execute the actual store procedure in callableStatment
+ * Store procedures method are expected to return a result
+ * - use executeUpdate for queries that do not.
+ * @throws DBException If the query fails or is an update query.
+ */
+ public synchronized void executeProcedure()
+ throws DBException {
+ long beginTimer = 0;
+ touch();
+
+ if (!isConnected) {
+ throw new DBException("Not connected to database " +
+ "- called connect method first (" +
+ myDescription + ", db/context '" +
+ getDataContext() + "')");
+ }
+
+ checkTimeOut();
+
+ try {
+ if (myConnection == null) {
+ throw new DBException("No current connection - " +
+ "connect to database failed: Unable to execute query:" +
+ "callableStatment" + " (" + myDescription +
+ ", db/context '" + getDataContext() + "')");
+ }
+
+ //
+ //More robust recovery. Some databases will throw a sqlexception
+ //if the resultset or statement is already closed
+ //
+ if (myResultSet != null) {
+ try {
+ myResultSet.close();
+ }
+ catch (SQLException ex) {
+ log.warn("Error closing resultset", ex);
+ }
+ myResultSet = null;
+ }
+ if (log.isDebugEnabled()) {
+ beginTimer = System.currentTimeMillis();
+ }
+
+ if (stmnt != null) {
+ try {
+ stmnt.close();
+ }
+ catch (SQLException ex) {
+ log.warn("Error closing statement", ex);
+ }
+ stmnt = null;
+ }
+
+
+ //
+ //If a callable statement has been created
+ //then we assume we want a callable statement to execute.
+ //
+ if (callableStatement != null ) {
+
+ if (sqlLog.isDebugEnabled()) {
+ sqlLog.debug("Connection " + getId() + " Executing: callableStatement" +
+ " on db '" + getDataContext() + "'");
+ }
+
+ // boolean hasResultSet = callableStatement.execute();
+ myResultSet = callableStatement.executeQuery();
+ // if (hasResultSet) {
+ // lastUpdateCount = callableStatement.getUpdateCount();
+ // myResultSet = callableStatement.getResultSet();
+ // }
+ } else {
+ throw new DBException("Unable to execute statement: callableStatment" +
+ " (" + myDescription + ", db/context '" +
+ getDataContext() + "')");
+ }
+
+ if (log.isDebugEnabled()) {
+ long endTimer = System.currentTimeMillis();
+ log.debug("Time to execute callableStatment'" + "' was " +
+ (endTimer - beginTimer) +
+ " milliseconds");
+ }
+ } catch (SQLException se) {
+ throw new DBException("Unable to execute statement: callableStatment" +
+ " (" + myDescription + ", db/context '" +
+ getDataContext() + "')", se);
+ }
+ } /* executeProcedure() */
+
+
/**
* Disconnect abandoned connections
***************
*** 1646,1678 ****
public String getString(int fieldNum)
throws DBException {
- checkTimeOut();
! if (myResultSet == null) {
! throw new DBException("[2]Null ResultSet object (" +
! myDescription + ")");
! }
! try {
! String resultValue = myResultSet.getString(fieldNum);
! if (resultValue == null) {
! return null;
! }
! return resultValue.trim();
! } catch (SQLException se) {
! if (strSQL == null) {
! strSQL = ("null");
! }
! throw new DBException("Error fetching string field " + fieldNum +
! ":Last SQL was:" + strSQL + " (" +
! myDescription + ")", se.getMessage());
! }
} /* getString(int) */
/**
! * Return the value in the named field as a string, trimming off
! * any trailing whitespace
*
* @param fieldName The desired column name
--- 1870,1952 ----
public String getString(int fieldNum)
throws DBException {
! checkTimeOut();
! if (myResultSet == null) {
! throw new DBException("[2]Null ResultSet object (" + myDescription + ")");
! }
! try {
! String resultValue = myResultSet.getString(fieldNum);
! if (resultValue == null) {
! return null;
! }
! return resultValue.trim();
! } catch (SQLException se) {
! if (strSQL == null) {
! strSQL = ("null");
! }
!
! throw new DBException("Error fetching string field " + fieldNum + ":Last SQL was:" + strSQL + " (" +
! myDescription + ")", se.getMessage());
! }
} /* getString(int) */
+ /**
+ * Return the numbered field as a string, not trimming any trailing spaces
+ *
+ * @param fieldNum The number of the desired field
+ * @return String The string field value
+ * @throws DBException If an error occurrs accessing the given field
+ */
+ public String getStringNoTrim(int fieldNum)
+ throws DBException {
+ checkTimeOut();
+
+ if (myResultSet == null) {
+ throw new DBException("Null ResultSet object (" + myDescription + ")");
+ }
+ try {
+ String resultValue = myResultSet.getString(fieldNum);
+
+ if (resultValue == null) {
+ // return "";
+ return null;
+ }
+
+ return resultValue;
+ } catch (SQLException se) {
+ throw new DBException("Error fetching string field " + fieldNum + ":Last SQL was:" + strSQL + " (" +
+ " (" + myDescription + ")", se.getMessage());
+ }
+ } /* getStringNoTrim(int) */
+
+
+ /**
+ * Return the value in the named field as a string, trimming off
+ * any trailing whitespace
+ *
+ * @param fieldName The desired column name
+ * @return String The value of the field as a string, less any trailing
+ * whitespace
+ * @throws DBException If the value cannot be accessed
+ */
+ public String getString(String fieldName)
+ throws DBException {
+
+ String resultValue = getStringNoTrim(fieldName);
+
+ if (resultValue == null) {
+ return null;
+ }
+
+ return resultValue.trim();
+ } /* getString(String) */
+
+
/**
! * Return the value in the named field as a string
*
* @param fieldName The desired column name
***************
*** 1681,1691 ****
* @throws DBException If the value cannot be accessed
*/
! public String getString(String fieldName)
throws DBException {
checkTimeOut();
if (myResultSet == null) {
! throw new DBException("[2]Null ResultSet object (" +
! myDescription + ")");
}
try {
--- 1955,1964 ----
* @throws DBException If the value cannot be accessed
*/
! public String getStringNoTrim(String fieldName)
throws DBException {
checkTimeOut();
if (myResultSet == null) {
! throw new DBException("[2]Null ResultSet object (" + myDescription + ")");
}
try {
***************
*** 1696,1700 ****
}
! return resultValue.trim();
} catch (SQLException se) {
if (strSQL == null) {
--- 1969,1973 ----
}
! return resultValue;
} catch (SQLException se) {
if (strSQL == null) {
***************
*** 1702,1740 ****
}
! throw new DBException("Error fetching string field " + fieldName +
! ":Last SQL was:" + strSQL + " (" +
myDescription + ")", se.getMessage());
}
! } /* getString(int) */
!
!
! /**
! * Return the numbered field as a string, not trimming any trailing spaces
! *
! * @param fieldNum The number of the desired field
! * @return String The string field value
! * @throws DBException If an error occurrs accessing the given field
! */
! public String getStringNoTrim(int fieldNum)
! throws DBException {
! checkTimeOut();
!
! if (myResultSet == null) {
! throw new DBException("Null ResultSet object (" + myDescription +
! ")");
! }
! try {
! String resultValue = myResultSet.getString(fieldNum);
!
! if (resultValue == null) {
! return "";
! }
- return resultValue;
- } catch (SQLException se) {
- throw new DBException("Error fetching string field " + fieldNum +
- " (" + myDescription + ")", se.getMessage());
- }
- } /* getStringNoTrim(int) */
/**
--- 1975,1983 ----
}
! throw new DBException("Error fetching string field " + fieldName + ":Last SQL was:" + strSQL + " (" +
myDescription + ")", se.getMessage());
}
! } /* getStringNoTrim(String) */
/**
***************
*** 2017,2020 ****
--- 2260,2296 ----
}
+ /**
+ * Return the Database current transaction mode set.
+ *
+ * author Yves Henri AMAIZO, Mon Dec 22 10:30:59 2003
+ * @since Expresso 5.3
+ *
+ * @return Expresso corrresponding values.
+ * @throws DBException
+ */
+ public int getTransactionMode()
+ throws DBException {
+ try {
+ int transactionMode = myConnection.getTransactionIsolation();
+ switch (transactionMode) {
+ case Connection.TRANSACTION_NONE :
+ return 0;
+ case Connection.TRANSACTION_READ_COMMITTED :
+ return transactionCommittedMode;
+ case Connection.TRANSACTION_READ_UNCOMMITTED :
+ return transactionUncommittedMode;
+ case Connection.TRANSACTION_REPEATABLE_READ :
+ return transactionRepeatableMode;
+ case Connection.TRANSACTION_SERIALIZABLE :
+ return transactionSerializableMode;
+ }
+ } catch (SQLException se) {
+ throw new DBException("Unable to retrieve transaction mode set " +
+ " for this connection", se);
+ }
+ return -1;
+ }
+
+
/**
* When this connection is used as part of a pool of connections,
***************
*** 2307,2309 ****
}
! } /* DBConnection */
--- 2583,2730 ----
}
!
! /**
! * Set Database transaction isolation mode.
! *
! * author Yves Henri AMAIZO, Mon Dec 22 10:30:59 2003
! * @since Expresso 5.3
! *
! * @throws DBException
! */
! public void setTransactionIsolation(int isolationMode)
! throws DBException {
! try {
! if (supportsTransactions()) {
! myConnection.setTransactionIsolation(isolationMode);
! }
! } catch (SQLException se) {
! throw new DBException("Unable to set Database transaction isolation mode set " +
! " for this connection", se);
! }
! }
!
! /**
! * Set EXPRESSO transaction mode for the current DBConnection.
! *
! * author Yves Henri AMAIZO, Mon Dec 22 10:30:59 2003
! * @since Expresso 5.3
! *
! * @throws DBException
! */
! public void setTransactionMode(int transactionMode) throws DBException {
!
! try {
! switch (transactionMode) {
! case TRANSACTION_NORMAL_MODE :
! setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
! case TRANSACTION_DIRTY_MODE :
! setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
! case TRANSACTION_RESTRICTIVE_MODE :
! setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
! case TRANSACTION_EXCLUSIVE_MODE :
! setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
! }
! } catch (DBException se) {
! throw new DBException("", se);
! }
! }
!
! /**
! * Set EXPRESSO transaction mode for the current DBConnection.
! *
! * author Yves Henri AMAIZO, Mon Dec 22 10:30:59 2003
! * @since Expresso 5.3
! *
! * @throws DBException
! */
! public void setTransactionCommittedMode() throws DBException {
!
! try {
! setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
! } catch (DBException se) {
! throw new DBException("", se);
! }
! }
!
! /**
! * Set EXPRESSO transaction mode for the current DBConnection.
! *
! * author Yves Henri AMAIZO, Mon Dec 22 10:30:59 2003
! * @since Expresso 5.3
! *
! * @throws DBException
! */
! public void setTransactionUncommittedMode() throws DBException {
!
! try {
! setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
! } catch (DBException se) {
! throw new DBException("", se);
! }
! }
!
! /**
! * Set EXPRESSO transaction mode for the current DBConnection.
! *
! * author Yves Henri AMAIZO, Mon Dec 22 10:30:59 2003
! * @since Expresso 5.3
! *
! * @throws DBException
! */
! public void setTransactionRepeatableMode() throws DBException {
!
! try {
! setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
! } catch (DBException se) {
! throw new DBException("", se);
! }
! }
!
! /**
! * Set EXPRESSO transaction mode for the current DBConnection.
! *
! * author Yves Henri AMAIZO, Mon Dec 22 10:30:59 2003
! * @since Expresso 5.3
! *
! * @throws DBException
! */
! public void setTransactionSerializableMode() throws DBException {
!
! try {
! setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
! } catch (DBException se) {
! throw new DBException("", se);
! }
! }
!
! /**
! * Trim or not trim parameter set for this connection
! *
! * @return boolean True if the connection not trim the persistence string.
! * false if the persistence string will have to be trimmed
! */
! public boolean isStringNotTrim() {
! return myJdbc.isStringNotTrim();
! } /* isStringNotTrimmed() */
!
! /**
! * Trim or not trim parameter set for this connection
! *
! * @return boolean True if the connection not trim the persistence string.
! * false if the persistence string will have to be trimmed
! */
! public boolean isTransactionNotActive() {
! return myJdbc.isTransactionNotActive();
! } /* isTransactionNotActive() */
!
!
! /**
! * @param config
! */
! public void setJDBCCondig(JDBCConfig config) {
! myJdbc = config;
! }
!
! }
!
! /* DBConnection */
More information about the cvs
mailing list