View Javadoc
1 /*** $Id: DAOJdbcActionProcessorImpl.java,v 1.1.1.1 2004/05/18 10:50:13 mochoa Exp $ */ 2 package org.j2ee.dao; 3 4 //sql 5 import java.sql.CallableStatement; 6 import java.sql.Connection; 7 import java.sql.PreparedStatement; 8 import java.sql.ResultSet; 9 import java.sql.SQLException; 10 11 import org.apache.log4j.Logger; 12 13 import org.j2ee.dao.errors.ErrorMapper; 14 import org.j2ee.dao.errors.ErrorMapperFactory; 15 // log4j 16 // DAO 17 18 /*** 19 * <br> 20 * Based on an example of book <i>Code Notes for J2EE, Edited by GREGORY BRILL, 21 * e-ISBN 0-679-64727-9</i> 22 * <br> 23 * Last modified $Date: 2004/05/18 10:50:13 $ 24 * @version $Revision: 1.1.1.1 $ 25 * @author jvlio (jvlio@users.sourceforge.net)*/ 26 public class DAOJdbcActionProcessorImpl implements DAOActionProcessor { 27 28 private static Logger log = Logger.getLogger(DAOJdbcActionProcessorImpl.class); 29 private Connection connection; 30 private boolean externalTransaction = false; 31 32 /*** 33 * Return an ErrorMapper object to translate SQLException or other kind of exception 34 * to a common error list code 35 * @return ErrorMapper 36 */ 37 public ErrorMapper getErrorMapper() { 38 return ErrorMapperFactory.getErrorMapper(ErrorMapperFactory.ORACLE); 39 } 40 41 public void process(DAOAction acmd) throws DAOException { 42 JdbcDAOAction cmd; 43 if (acmd instanceof JdbcDAOAction) 44 cmd = (JdbcDAOAction)acmd; 45 else 46 throw new DAOException("Expected a JdbcDAOAction instance"); 47 if (log.isDebugEnabled()) 48 log.debug(".process - sql_stmt="+cmd.getSQL()); 49 long pproc = System.currentTimeMillis();//profiling; 50 long pexec = 0; 51 PreparedStatement pstmt = null; 52 ResultSet rs = null; 53 try { 54 Connection con = getConnection(); 55 if (con == null) // Sanity checks 56 throw new IllegalArgumentException("Expected a JDBC connection, please use setConnection first"); 57 if (!externalTransaction) { 58 con.setAutoCommit(false); // Change to autocommit false during the executing of this process 59 log.debug(".process - Start an internal transaction in this Processor."); 60 } 61 62 if (cmd.isCaching()) { 63 pstmt = cmd.getPreparedStatement(); 64 if (pstmt==null) { 65 pstmt = (cmd.isCall())? con.prepareCall(cmd.getSQL()) : con.prepareStatement(cmd.getSQL()); 66 cmd.cachPreparedStatement(pstmt); 67 } 68 } 69 else 70 pstmt = (cmd.isCall())? con.prepareCall(cmd.getSQL()) : con.prepareStatement(cmd.getSQL()); 71 72 Object[] params = cmd.getParameters(); 73 int[] dataTypes = cmd.getDataTypesForParams(); 74 if (params!=null && dataTypes!=null) 75 for (int i = 1; i <= params.length; i++) { 76 if (params[i-1] == null) 77 pstmt.setNull(i, dataTypes[i-1]); 78 else 79 pstmt.setObject(i, params[i-1]); 80 } 81 //execute 82 if (cmd.isQuery()) {//query 83 pexec = System.currentTimeMillis();//profiling 84 rs = pstmt.executeQuery(); 85 pexec = System.currentTimeMillis()-pexec;//profiling 86 cmd.handleResults(rs); 87 } 88 else 89 if (cmd.isCall()) { //call 90 CallableStatement cstmt = (CallableStatement)pstmt; 91 dataTypes = cmd.getDataTypesForOutParams(); 92 for (int i = 1; i <= dataTypes.length; i++) 93 if (dataTypes[i-1] != AbstractJdbcDAOAction.NO_OUT_PARAM) 94 cstmt.registerOutParameter(i,dataTypes[i-1]); 95 pexec = System.currentTimeMillis();//profiling 96 cstmt.execute(); 97 pexec = System.currentTimeMillis()-pexec;//profiling 98 cmd.handleResults(cstmt); 99 } 100 else { //update 101 pexec = System.currentTimeMillis();//profiling 102 int rowsAffected = pstmt.executeUpdate(); 103 pexec = System.currentTimeMillis()-pexec;//profiling 104 cmd.handleResults(rowsAffected); 105 } 106 if (!externalTransaction) { 107 log.debug(".process - Commit an internal transaction in this Processor."); 108 con.commit(); 109 con.setAutoCommit(true); // Leave the connection in autocommit mode (by default) 110 } 111 if (log.isDebugEnabled()) 112 log.debug(".process - Hibernate elapsed time = "+pexec+" ms."); 113 } catch (Exception e) { 114 if (!externalTransaction) { 115 log.debug(".process - Rollback an internal transaction in this Processor."); 116 try { 117 Connection con = getConnection(); 118 con.rollback(); 119 con.setAutoCommit(true); // Leave the connection in autocommit mode (by default) 120 } catch (SQLException sqe) { 121 log.error("There where errors on Rollback work",sqe); 122 } 123 } 124 log.error("DAOJdbcActionProcessorImpl error on process",e); 125 throw new DAOException("DAOJdbcActionProcessorImpl error on process",e); 126 } finally { 127 try { 128 if (!cmd.isCaching() && rs!=null) { 129 log.debug(".process - closing rs"); 130 rs.close(); 131 } 132 if (!cmd.isCaching() && pstmt!=null) { 133 log.debug(".process - closing pstmt"); 134 pstmt.close(); 135 } 136 } catch (SQLException e) { 137 log.error("There where error closing ResulSet or Prepared Stamente",e); 138 } 139 } 140 pproc = System.currentTimeMillis()-pproc;//profiling 141 if (log.isDebugEnabled()) 142 log.debug(".process - Processor elapsed time = "+pproc+" ms."); 143 } 144 145 /*** 146 */ 147 public Connection getConnection() { return connection; } 148 149 /*** 150 */ 151 public void setConnection(Connection connection) { this.connection = connection; } 152 153 /*** 154 * get/start a DAO Session object to be used for external controled action. 155 * The processor object hold the internal representation for a session. 156 * Toplink and Hibernta will hold the Session object, JDBC will use a Connection 157 * @throws DAOException if there are errors open the session 158 */ 159 public void getSession() throws DAOException {}; 160 161 /*** 162 * Start a new DAO transaction. 163 * This method is usefull when you need to manage Macro Actions. 164 * The first action takes the session and the transaction a pass it to the others. 165 * The processor instance will hold the Transaction object, 166 * - Toplink implementation will use UnitOfWork object. 167 * - Hibernate implementation will use Transaction object. 168 * - JDBC will use his internal JDBC Connection 169 * @throws DAOException 170 */ 171 public void beginTransaction() throws DAOException { 172 Connection con = getConnection(); 173 if (con == null) // Sanity checks 174 throw new IllegalArgumentException("Expected a JDBC connection, please use setConnection first"); 175 try { 176 con.setAutoCommit(false); 177 externalTransaction = true; 178 } catch (SQLException sqe) { 179 externalTransaction = false; 180 log.error("Can't set auto commit to false",sqe); 181 throw new DAOException("Can't set auto commit to false",sqe); 182 } 183 } 184 185 /*** 186 * commit a started transaction. 187 * @throws DAOException 188 */ 189 public void commitTransaction() throws DAOException { 190 Connection con = getConnection(); 191 if (con == null) // Sanity checks 192 throw new IllegalArgumentException("Expected a JDBC connection, please use setConnection first"); 193 try { 194 con.commit(); 195 con.setAutoCommit(true); // Leave connection in autocommit mode (by default) 196 } catch (SQLException sqe) { 197 log.error("Can't commit the transaction",sqe); 198 throw new DAOException("Can't commit the transaction",sqe); 199 } finally { 200 externalTransaction = false; 201 } 202 } 203 204 /*** 205 * rollback a started transaction. 206 * @throws DAOException 207 */ 208 public void rollbackTransaction() throws DAOException { 209 Connection con = getConnection(); 210 if (con == null) // Sanity checks 211 throw new IllegalArgumentException("Expected a JDBC connection, please use setConnection first"); 212 try { 213 con.rollback(); 214 con.setAutoCommit(true); // Leave connection in autocommit mode (by default) 215 } catch (SQLException sqe) { 216 log.error("Can't rollback the transaction",sqe); 217 throw new DAOException("Can't rollback the transaction",sqe); 218 } finally { 219 externalTransaction = false; 220 } 221 } 222 223 /*** 224 * close a started session 225 * @throws DAOException 226 */ 227 public void closeSession() throws DAOException {}; 228 }

This page was automatically generated by Maven