1 package org.enhydra.barracuda.testbed.workbench.jdbc; 2 3 import java.awt.*; 4 import java.sql.*; 5 import java.util.*; 6 7 15 public class Database { 16 17 private Connection conn = null; 18 private String jdbcDriverName = ""; 19 private String jdbcServerURL = ""; 20 private String dbUserName = ""; 21 private String dbPassword = ""; 22 private Properties props = new Properties(); 23 private Driver theJDBCDriver = null; 24 private DriverManager dmanager = null; 25 private int commitLevel = -1; 26 private String userName = ""; 27 28 36 public Database (String ijdbcDriverName, String ijdbcServerURL, String idbUserName, String idbPassword) { 37 jdbcDriverName = ijdbcDriverName; 38 jdbcServerURL = ijdbcServerURL; 39 dbUserName = idbUserName; 40 dbPassword = idbPassword; 41 } 42 43 48 public void connectServer() throws java.beans.PropertyVetoException { 49 dmanager.setLoginTimeout(20); 51 52 try { 54 theJDBCDriver = (Driver)Class.forName(jdbcDriverName).newInstance(); 56 57 props.put("user",dbUserName); 59 props.put("password",dbPassword); 60 conn = makeConnection(theJDBCDriver, jdbcServerURL, props); 61 setAutoCommit(true); 62 } catch (SQLException se) { 63 String message = null; 64 message = se.toString(); 65 throw new java.beans.PropertyVetoException ("Error Connecting to Database: "+message,null); 66 } catch (Exception e) { 67 throw new java.beans.PropertyVetoException ("Error Connecting to Database: "+e,null); 68 } 69 } 70 71 80 private static synchronized Connection makeConnection (Driver theJDBCDriver, String jdbcServerURL, Properties props) throws SQLException { 81 return theJDBCDriver.connect(jdbcServerURL, props); 82 } 83 84 89 public void disconnectServer() throws java.beans.PropertyVetoException { 90 try { 93 if (conn!=null) conn.close(); 94 } catch (Exception e) { 95 throw new java.beans.PropertyVetoException ("Error Disconnecting from Database:"+e,null); 96 } finally { 97 conn = null; 98 } 99 } 100 101 106 public boolean isConnected() { 107 return conn!=null; 108 } 109 110 117 public PreparedStatement prepareStatement(String s) throws SQLException { 118 for (;;) { 119 try { 120 while (!isConnected()) { 123 try { 124 connectServer(); 125 } catch (java.beans.PropertyVetoException pve) { 126 System.out.println ("Server not responding..."); 127 try {Thread.sleep(200);} 128 catch (InterruptedException ie) {} 129 } finally { 130 if (isConnected()) System.out.println ("Connection reestablished..."); 131 } 132 } 133 134 return conn.prepareStatement(s); 136 } catch (SQLException se) { 137 String err = se.getSQLState(); 138 if (err.equals("08S01")) { 142 try {disconnectServer();} 143 catch (java.beans.PropertyVetoException pve) {} 144 } else { 145 throw se; 146 } 147 } 148 } 149 } 150 151 157 public Statement createStatement() throws SQLException { 158 for (;;) { 159 try { 160 while (!isConnected()) { 163 try {connectServer();} 164 catch (java.beans.PropertyVetoException pve) { 165 System.out.println ("Server not responding..."); 166 try {Thread.sleep(200);} 167 catch (InterruptedException ie) {} 168 } finally { 169 if (isConnected()) System.out.println ("Connection reestablished..."); 170 } 171 } 172 173 return conn.createStatement(); 175 } catch (SQLException se) { 176 String err = se.getSQLState(); 177 if (err.equals("08S01")) { 181 try {disconnectServer();} 182 catch (java.beans.PropertyVetoException pve) {} 183 } else { 184 throw se; 185 } 186 } 187 } 188 } 189 190 196 public Connection getConnection() throws java.beans.PropertyVetoException { 197 return getConnection(false); 198 } 199 200 207 public Connection getConnection(boolean verify) throws java.beans.PropertyVetoException { 208 if (verify) { 211 try {verifyConnection();} 212 catch (SQLException se) {throw new java.beans.PropertyVetoException ("Error getting the Connection to Database:"+se,null);} 213 } 214 215 return conn; 217 } 218 219 225 public void setConnection (Connection c) { 226 conn = c; 227 } 228 229 234 public void verifyConnection() throws SQLException { 235 verifyConnection("select count(*) from metadata"); } 237 238 public void verifyConnection(String s) throws SQLException { 239 this.prepareStatement(s).close(); 240 } 241 242 245 public void resetConnection() { 246 conn = null; 247 } 248 249 254 public void commit() throws SQLException { 255 int cl = getCommitLevel(); 256 if (cl>0) return; 257 258 if (!getAutoCommit()) { 259 conn.commit(); 260 resetCommitLevel(); } 262 } 263 264 269 public void rollback() throws SQLException { 270 int cl = getCommitLevel(); 271 if (cl>0) return; 272 273 if (!getAutoCommit()) { 274 conn.rollback(); 275 resetCommitLevel(); } 277 } 278 279 282 public void resetCommitLevel() { 283 commitLevel = -1; 285 } 286 287 290 public int getCommitLevel() { 291 return commitLevel; 293 } 294 295 300 public void setAutoCommit(boolean val) { 301 if (commitLevel<0) { 303 try {conn.setAutoCommit(val);} 304 catch (SQLException se) { 305 System.out.println ("Error setting AutoCommit..."); 306 Throwable th = new Throwable (); 307 th.printStackTrace(); 308 } 309 } 310 311 if (val==false) commitLevel++; else if (commitLevel>-1) commitLevel--; } 322 323 328 public boolean getAutoCommit() { 329 try {return conn.getAutoCommit();} 330 catch (SQLException se) { 331 System.out.println ("Error getting AutoCommit..."); 332 Throwable th = new Throwable (); 333 th.printStackTrace(); 334 } 335 return false; 336 } 337 338 339 344 public String getUserName() { 345 return userName; 346 } 347 348 353 public void setUserName(String s) { 354 userName = s; 355 } 356 } 357 | Popular Tags |