1 19 package org.openbravo.wad; 20 21 22 import java.sql.*; 23 import java.io.*; 24 import org.apache.xerces.parsers.DOMParser; 25 import org.xml.sax.*; 26 import org.w3c.dom.*; 27 import org.openbravo.exception.*; 28 import org.openbravo.database.*; 29 import org.apache.log4j.Logger; 30 31 public class WadConnection implements ConnectionProvider { 32 static Logger log4j = Logger.getLogger(WadConnection.class); 33 protected Connection myPool; 34 String defaultPoolName = ""; 35 String bbdd = ""; 36 String rdbms = ""; 37 String contextName = "openbravo"; 38 39 public WadConnection (String xmlPoolFile) { 40 if (myPool == null) { 41 try { 42 connect(xmlPoolFile); 43 } catch (Exception e) { 44 e.printStackTrace(); 45 } 46 } 47 } 48 49 public void connect(String file) throws ClassNotFoundException , SQLException { 50 if (log4j.isDebugEnabled()) log4j.debug("Creating Connection"); 51 try { 52 DOMParser parser = new DOMParser(); 53 try { 54 parser.parse((file.toUpperCase().startsWith("FILE:///")?"":"file:///") + file); 55 } catch (SAXException se) { 56 log4j.error(se); 57 throw new SQLException("Couldn't load parse for pool file " + file); 58 } catch (IOException ioe) { 59 log4j.error(ioe); 60 throw new SQLException("Couldn't load pool file " + file + " for input/output operations"); 61 } 62 Document xmlPool = parser.getDocument(); 63 NodeList nodeList = xmlPool.getElementsByTagName("pool"); 64 log4j.debug("Pool's elements: " + nodeList.getLength()); 65 Node child = null; 66 for (int i=0;i<nodeList.getLength();i++) { 67 Node pool = nodeList.item(i); 68 NamedNodeMap atributos = pool.getAttributes(); 69 String poolName = atributos.item(0).getNodeValue(); 70 if (log4j.isDebugEnabled()) log4j.debug("poolName: " + poolName); 71 this.defaultPoolName = poolName; 72 73 child = pool.getFirstChild(); 74 child = child.getNextSibling(); 75 String dbDriver = ((Text)child.getFirstChild()).getData(); 76 if (log4j.isDebugEnabled()) log4j.debug("dbDriver: " + dbDriver); 77 Class.forName(dbDriver); 78 child = child.getNextSibling(); 79 child = child.getNextSibling(); 80 String dbServer = ((Text)child.getFirstChild()).getData(); 81 if (log4j.isDebugEnabled()) log4j.debug("dbServer: " + dbServer); 82 this.bbdd = dbServer; 83 child = child.getNextSibling(); 84 child = child.getNextSibling(); 85 Text textDbLogin = (Text)child.getFirstChild(); 86 String dbLogin; 87 if (textDbLogin != null) { 88 dbLogin = textDbLogin.getData(); 89 if (log4j.isDebugEnabled()) log4j.debug("dbLogin: " + dbLogin); 90 } else dbLogin = null; 91 child = child.getNextSibling(); 92 child = child.getNextSibling(); 93 Text textDbPassword = (Text)child.getFirstChild(); 94 String dbPassword; 95 if (textDbPassword != null) { 96 dbPassword = textDbPassword.getData(); 97 if (log4j.isDebugEnabled()) log4j.debug("dbPassword: " + dbPassword); 98 } else dbPassword = null; 99 this.myPool=DriverManager.getConnection(dbServer, dbLogin, dbPassword); 100 this.myPool.setAutoCommit(true); 101 102 child = child.getNextSibling(); 103 child = child.getNextSibling(); 104 int minConns = Integer.valueOf(((Text)child.getFirstChild()).getData()).intValue(); 105 if (log4j.isDebugEnabled()) log4j.debug("minConns: " + minConns); 106 child = child.getNextSibling(); 107 child = child.getNextSibling(); 108 int maxConns = Integer.valueOf(((Text)child.getFirstChild()).getData()).intValue(); 109 if (log4j.isDebugEnabled()) log4j.debug("maxConns: " + maxConns); 110 child = child.getNextSibling(); 111 child = child.getNextSibling(); 112 double maxConnTime = Double.valueOf(((Text)child.getFirstChild()).getData()).doubleValue(); 113 if (log4j.isDebugEnabled()) log4j.debug("maxConnTime: " + Double.toString(maxConnTime)); 114 child = child.getNextSibling(); 115 child = child.getNextSibling(); 116 String dbSessionConfig = ((Text)child.getFirstChild()).getData(); 117 if (log4j.isDebugEnabled()) log4j.debug("dbSessionConfig: " + dbSessionConfig); 118 child = child.getNextSibling(); 119 child = child.getNextSibling(); 120 String myrdbms = ((Text)child.getFirstChild()).getData(); 121 if (log4j.isDebugEnabled()) log4j.debug("rdbms: " + myrdbms); 122 123 124 this.rdbms = myrdbms; 125 } 126 } 127 catch (Exception e) { 128 log4j.error(e); 129 throw new SQLException("Failed when creating database connections pool"); 130 } 131 } 132 133 public void destroy() { 134 try { 135 if (myPool!=null) myPool.close(); 136 myPool = null; 137 } catch(SQLException e){ 138 log4j.error("SQL error in closeConnection: " + e); 139 } 140 } 141 142 public Connection getConnection() throws NoConnectionAvailableException { 143 return this.myPool; 144 } 145 146 public String getRDBMS() { 147 return rdbms; 148 } 149 150 public boolean releaseConnection(Connection conn) { 151 if (conn==null) return false; 152 try { 153 conn.setAutoCommit(true); 154 } catch (Exception ex) { 156 ex.printStackTrace(); 157 return false; 158 } 159 return true; 160 } 161 162 public Connection getTransactionConnection() throws NoConnectionAvailableException, SQLException { 163 Connection conn = getConnection(); 164 if (conn==null) throw new NoConnectionAvailableException("CouldnĀ“t get an available connection"); 165 conn.setAutoCommit(false); 166 return conn; 167 } 168 169 public void releaseCommitConnection(Connection conn) throws SQLException { 170 if (conn==null) return; 171 conn.commit(); 172 releaseConnection(conn); 173 } 174 175 public void releaseRollbackConnection(Connection conn) throws SQLException { 176 if (conn==null) return; 177 conn.rollback(); 178 releaseConnection(conn); 179 } 180 181 public PreparedStatement getPreparedStatement(String poolName, String SQLPreparedStatement) throws Exception { 182 return getPreparedStatement(SQLPreparedStatement); 183 } 184 185 public PreparedStatement getPreparedStatement(String SQLPreparedStatement) throws Exception { 186 if (log4j.isDebugEnabled()) log4j.debug("connection requested"); 187 Connection conn = getConnection(); 188 if (log4j.isDebugEnabled()) log4j.debug("connection established"); 189 return getPreparedStatement(conn, SQLPreparedStatement); 190 } 191 192 public PreparedStatement getPreparedStatement(Connection conn, String SQLPreparedStatement) throws SQLException { 193 if (conn == null || SQLPreparedStatement==null || SQLPreparedStatement.equals("")) return null; 194 PreparedStatement ps = null; 195 try { 196 if (log4j.isDebugEnabled()) log4j.debug("preparedStatement requested"); 197 ps = conn.prepareStatement(SQLPreparedStatement, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); 198 if (log4j.isDebugEnabled()) log4j.debug("preparedStatement received"); 199 } catch(SQLException e) { 200 log4j.error("getPreparedStatement: " + SQLPreparedStatement + "\n" + e); 201 releaseConnection(conn); 202 throw e; 203 } 204 return(ps); 205 } 206 207 public CallableStatement getCallableStatement(String poolName, String SQLCallableStatement) throws Exception { 208 return getCallableStatement(SQLCallableStatement); 209 } 210 211 public CallableStatement getCallableStatement(String SQLCallableStatement) throws Exception { 212 Connection conn = getConnection(); 213 return getCallableStatement(conn, SQLCallableStatement); 214 } 215 216 public CallableStatement getCallableStatement(Connection conn, String SQLCallableStatement) throws SQLException { 217 if (conn==null || SQLCallableStatement==null || SQLCallableStatement.equals("")) return null; 218 CallableStatement cs = null; 219 try { 220 cs = conn.prepareCall(SQLCallableStatement); 221 } catch(SQLException e) { 222 log4j.error("getCallableStatement: " + SQLCallableStatement + "\n" + e); 223 releaseConnection(conn); 224 throw e; 225 } 226 return(cs); 227 } 228 229 public Statement getStatement(String name) throws Exception { 230 return getStatement(); 231 } 232 233 public Statement getStatement() throws Exception { 234 Connection conn = getConnection(); 235 return getStatement(conn); 236 } 237 238 public Statement getStatement(Connection conn) throws SQLException { 239 if (conn == null) return null; 240 try { 241 return(conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)); 242 } catch(SQLException e) { 243 log4j.error("getStatement: " + e); 244 releaseConnection(conn); 245 throw e; 246 } 247 } 248 249 public void releasePreparedStatement(PreparedStatement preparedStatement) throws SQLException { 250 if (preparedStatement == null) return; 251 Connection conn = null; 252 try { 253 conn = preparedStatement.getConnection(); 254 preparedStatement.close(); 255 releaseConnection(conn); 256 } catch (SQLException e) { 257 log4j.error("releasePreparedStatement: " + e); 258 releaseConnection(conn); 259 throw e; 260 } 261 } 262 263 public void releaseCallableStatement(CallableStatement callableStatement) throws SQLException { 264 if (callableStatement == null) return; 265 Connection conn = null; 266 try { 267 conn = callableStatement.getConnection(); 268 callableStatement.close(); 269 releaseConnection(conn); 270 } catch (SQLException e) { 271 log4j.error("releaseCallableStatement: " + e); 272 releaseConnection(conn); 273 throw e; 274 } 275 } 276 277 public void releaseStatement(Statement statement) throws SQLException { 278 if (statement == null) return; 279 Connection conn = null; 280 try { 281 conn = statement.getConnection(); 282 statement.close(); 283 releaseConnection(conn); 284 } catch (SQLException e) { 285 log4j.error("releaseStatement: " + e); 286 releaseConnection(conn); 287 throw e; 288 } 289 } 290 291 public void releaseTransactionalStatement(Statement statement) throws SQLException { 292 if (statement == null) return; 293 statement.close(); 294 } 295 296 public void releaseTransactionalPreparedStatement(PreparedStatement preparedStatement) throws SQLException { 297 if (preparedStatement==null) return; 298 preparedStatement.close(); 299 } 300 301 304 public String getStatus() { 305 StringBuffer strResultado = new StringBuffer (); 306 strResultado.append("Not implemented yet"); 307 return strResultado.toString(); 308 }} 310 | Popular Tags |