From: <th...@us...> - 2003-12-08 10:03:32
|
Update of /cvsroot/jaxme/JaxMe/src/runtime/de/ispsoft/jaxme In directory sc8-pr-cvs1:/tmp/cvs-serv2997/src/runtime/de/ispsoft/jaxme Modified Files: JMJdbcManager.java Log Message: added some import statements and some debug output for time consumtion of sql statements Index: JMJdbcManager.java =================================================================== RCS file: /cvsroot/jaxme/JaxMe/src/runtime/de/ispsoft/jaxme/JMJdbcManager.java,v retrieving revision 1.10 retrieving revision 1.11 diff -u -d -r1.10 -r1.11 --- JMJdbcManager.java 11 Nov 2003 12:43:45 -0000 1.10 +++ JMJdbcManager.java 8 Dec 2003 10:03:29 -0000 1.11 @@ -4,9 +4,15 @@ import java.io.CharArrayReader; import java.io.StringReader; import java.sql.Connection; +import java.sql.DriverManager; import java.sql.PreparedStatement; +import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Timestamp; import java.sql.Types; +import java.util.Date; + +import org.apache.log4j.Category; import org.xml.sax.SAXException; import org.w3c.dom.Node; import org.w3c.dom.Element; @@ -18,6 +24,9 @@ * @author <a href="mailto:jo...@is...">Jochen Wiedmann</a> */ public abstract class JMJdbcManager extends JMManagerImpl { + + protected Category cat = Category.getInstance(JMJdbcManager.class.getName()); + /** <p>Name of our private configuration node (<samp>JMJdbcManager</samp> in * namespace <code>JMManagerFactoryImpl.NAMESPACE_URI</code>)</p> */ @@ -108,11 +117,11 @@ try { Class.forName(driver); // Verify the connection - java.sql.Connection conn = getConnection(); + Connection conn = getConnection(); conn.close(); } catch (ClassNotFoundException e) { throw new SAXException("Class " + driver + " could not be loaded", e); - } catch (java.sql.SQLException e) { + } catch (SQLException e) { throw new SAXException("Could not verify the JDBC connection " + url, e); } } @@ -120,12 +129,12 @@ /** <p>Returns an open JDBC connection</p> */ - public java.sql.Connection getConnection() - throws java.sql.SQLException, org.xml.sax.SAXException { + public Connection getConnection() + throws SQLException, SAXException { if (user != null && user.length() > 0) { - return java.sql.DriverManager.getConnection(url, user, password); + return DriverManager.getConnection(url, user, password); } else { - return java.sql.DriverManager.getConnection(url); + return DriverManager.getConnection(url); } } @@ -135,14 +144,19 @@ pConn.close(); } - protected void executeQueries(java.sql.Connection pConn, - java.sql.PreparedStatement[] pStmt) - throws java.sql.SQLException { + protected void executeQueries(Connection pConn, PreparedStatement[] pStmt) + throws SQLException { for (int i = 0; i < pStmt.length; i++) { - java.sql.PreparedStatement stmt = pStmt[i]; + Date start = new Date(); + PreparedStatement stmt = pStmt[i]; stmt.executeUpdate(); pStmt[i] = null; stmt.close(); + if(cat.isDebugEnabled()){ + Date end = new Date(); + cat.debug("Statement succesfully executed (took "+ + (end.getTime()-start.getTime())+" ms)"); + } } } @@ -172,9 +186,8 @@ /** <p>Creates a new primary key for the element. Does nothing, * to be overwritten by subclasses.</p> */ - protected void newPrimaryKey(java.sql.Connection pConn, - JMAnyElement pElement) - throws java.sql.SQLException { + protected void newPrimaryKey(Connection pConn, JMAnyElement pElement) + throws SQLException { } /** <p>Calls <code>newPrimaryKey()</code> and inserts the given @@ -189,8 +202,8 @@ private static final int CRUD_DELETE = 2; private void crud(JMAnyElement pElement, int pOp) throws SAXException { int createPrimaryKey = CREATE_KEY_DONT; - java.sql.Connection conn = null; - java.sql.PreparedStatement[] stmts = null; + Connection conn = null; + PreparedStatement[] stmts = null; try { conn = getConnection(); switch (pOp) { @@ -215,17 +228,17 @@ if (createPrimaryKey == CREATE_KEY_POST_INSERT) { newPrimaryKey(conn, pElement); } - java.sql.Connection myconn = conn; + Connection myconn = conn; conn = null; releaseConnection(myconn); - } catch (java.sql.SQLException e) { + } catch (SQLException e) { throw new SAXException("SQL Exception: Error code = " + e.getErrorCode() + ", error state = " + e.getSQLState() + ", error message = " + e.getMessage(), e); } finally { if (stmts != null) { for (int i = 0; i < stmts.length; i++) { - java.sql.PreparedStatement pstmt = stmts[i]; + PreparedStatement pstmt = stmts[i]; if (pstmt != null) { try { pstmt.close(); } catch (Exception e) {} } @@ -276,9 +289,10 @@ Object[] pPlaceHolderArgs, int pStart, int pMax) throws SAXException { String query = getQuery(pQuery); - java.sql.Connection conn = null; - java.sql.PreparedStatement stmt = null; - java.sql.ResultSet rs = null; + Connection conn = null; + PreparedStatement stmt = null; + ResultSet rs = null; + Date start = new Date(); try { conn = getConnection(); stmt = getPreparedStatement(conn, query, pPlaceHolderArgs); @@ -288,16 +302,21 @@ init(rs, element); pObserver.notify(element); } - java.sql.ResultSet myrs = rs; + ResultSet myrs = rs; rs = null; myrs.close(); - java.sql.PreparedStatement mystmt = stmt; + PreparedStatement mystmt = stmt; stmt = null; mystmt.close(); - java.sql.Connection myconn = conn; + Connection myconn = conn; conn = null; releaseConnection(myconn); - } catch (java.sql.SQLException e) { + if(cat.isInfoEnabled()){ + Date end = new Date(); + cat.debug("Statement succesfully executed (DURATION="+ + (end.getTime()-start.getTime())+"ms)"); + } + } catch (SQLException e) { throw new SAXException("Failed to perform query " + query, e); } finally { if (rs != null) { try { rs.close(); } catch (Exception e) {} } @@ -345,8 +364,8 @@ /** <p>Initializes the element from the resultset. In other words, reads * one row from the result set.</p> */ - protected abstract void init(java.sql.ResultSet rs, JMJdbcElement pElement) - throws java.sql.SQLException, org.xml.sax.SAXException; + protected abstract void init(ResultSet rs, JMJdbcElement pElement) + throws SQLException, SAXException; /** <p>Returns the field list to build an SQL SELECT statement.</p> */ @@ -363,27 +382,25 @@ /** <p>Returns an array of prepared statements, that are being * performed to insert the element into the database.</p> */ - protected java.sql.PreparedStatement[] getInsertQueries(java.sql.Connection pConn, + protected PreparedStatement[] getInsertQueries(Connection pConn, JMJdbcElement pElement) - throws org.xml.sax.SAXException, java.sql.SQLException { + throws SAXException, SQLException { throw new SAXException("Not implemented"); } /** <p>Returns an array of prepared statements, that are being * performed to update the element in the database.</p> */ - protected java.sql.PreparedStatement[] getUpdateQueries(java.sql.Connection pConn, - JMJdbcElement pElement) - throws org.xml.sax.SAXException, java.sql.SQLException { + protected PreparedStatement[] getUpdateQueries(Connection pConn, + JMJdbcElement pElement) throws SAXException, SQLException { throw new SAXException("Not implemented"); } /** <p>Returns an array of prepared statements, that are being * performed to delete the element in the database.</p> */ - protected java.sql.PreparedStatement[] getDeleteQueries(java.sql.Connection pConn, - JMJdbcElement pElement) - throws org.xml.sax.SAXException, java.sql.SQLException { + protected PreparedStatement[] getDeleteQueries(Connection pConn, + JMJdbcElement pElement) throws SAXException, SQLException { throw new SAXException("Not implemented"); } @@ -399,9 +416,8 @@ /** <p>Executes the given statement.</p> */ - public void executeStatement(String pQuery, - Object[] pPlaceHolderArgs) - throws org.xml.sax.SAXException { + public void executeStatement(String pQuery, Object[] pPlaceHolderArgs) + throws SAXException { Connection conn = null; PreparedStatement stmt = null; try { @@ -414,8 +430,8 @@ Connection myconn = conn; conn = null; releaseConnection(myconn); - } catch (java.sql.SQLException e) { - throw new org.xml.sax.SAXException("SQLException: " + e.getMessage(), e); + } catch (SQLException e) { + throw new SAXException("SQLException: " + e.getMessage(), e); } finally { if (stmt != null) { try { stmt.close(); } catch (Exception e) {} } if (conn != null) { try { releaseConnection(conn); } catch (Exception e) {} } @@ -425,9 +441,7 @@ /** <p>Prepares the placeholders of the given statement.</p> */ protected PreparedStatement getPreparedStatement(Connection pConn, - String pQuery, - Object[] pPlaceHolderArgs) - throws java.sql.SQLException { + String pQuery, Object[] pPlaceHolderArgs) throws SQLException { PreparedStatement stmt = pConn.prepareStatement(pQuery); if (pPlaceHolderArgs != null) { int i = 1; @@ -464,11 +478,11 @@ stmt.setString(i++, (String) o); break; case Types.TIMESTAMP: - if (o instanceof java.sql.Timestamp) { - stmt.setTimestamp(i++, (java.sql.Timestamp) o); + if (o instanceof Timestamp) { + stmt.setTimestamp(i++, (Timestamp) o); } else { - java.util.Date d = (java.util.Date) o; - stmt.setTimestamp(i++, new java.sql.Timestamp(d.getTime())); + Date d = (Date) o; + stmt.setTimestamp(i++, new Timestamp(d.getTime())); } break; case Types.BLOB: |