📄 dbwatchertester.java
字号:
assertEquals(tableName + ": entry '" + count + "' name ", rs1.getString(2), rs2.getString(2)); assertEquals(tableName + ": entry '" + count + "' surname ", rs1.getString(3), rs2.getString(3)); assertEquals(tableName + ": entry '" + count + "' mail ", rs1.getString(4), rs2.getString(4)); assertEquals(tableName + ": entry '" + count + "' photo ", rs1.getString(5), rs2.getString(5)); } } finally { if (st1 != null) st1.close(); if (st2 != null) st2.close(); conn.commit(); } System.out.println("COMPARISON OF TABLES '" + tableName + "' SUCCESSFULLY COMPLETED: entries '" + count + "' "); } private void checkReplItems(Connection conn) throws Exception { if (!this.commitCheck) return; Statement st = null; int count = 0; try { String sql = "SELECT * FROM replitems"; st = conn.createStatement(); ResultSet rs = st.executeQuery(sql); Iterator iter = this.operations.iterator(); String oldTransId = null; while (true) { boolean cont1 = rs.next(); boolean cont2 = iter.hasNext(); if (cont1 != cont2) { System.err.println("===================================="); for (int i=0; i < this.operations.size(); i++) { System.err.println(this.operations.get(i)); } System.err.println("===================================="); throw new Exception("checkReplItems failed because different length rs='" + cont1 + "' sent='" + cont2 + "'"); } if (!cont1) break; count++; String newTransId = rs.getString(2); String completeOp = (String)iter.next(); String op = completeOp.substring(0, completeOp.indexOf(' ')); try { assertEquals("entry '" + count + "' has wrong action ", rs.getString(6), op); if (count != 1) assertEquals("entry '" + count + "' has wrong transaction Id ", oldTransId, newTransId); oldTransId = newTransId; } catch (Exception ex) { System.err.println("===================================="); for (int i=0; i < this.operations.size(); i++) { System.err.println(this.operations.get(i)); } System.err.println("===================================="); throw ex; } } this.operations.clear(); sql = "DELETE FROM replitems"; st.close(); st = conn.createStatement(); st.executeUpdate(sql); st.close(); } finally { if (st != null) st.close(); conn.commit(); } } private void commit(Connection conn) { try { if (conn != null) { if (this.exceptionOccured) { this.operations.clear(); this.exceptionOccured = false; conn.rollback(); } else conn.commit(); } } catch (Exception ex) { ex.printStackTrace(); } } public void run() { Connection conn = null; if (this.commitCheck) { log.warning("'commitCheck' is choosen: Entries on 'replitems' will be deleted after each commit. DbWatcher must be switched off !!!"); } try { conn = this.dbPool.reserve(); conn.setAutoCommit(false); this.compareCount = 1; for (int i=0; i < this.nmax; i++) { try { int choice = this.random.nextInt(4); switch (choice) { case 0 : doInsert(conn); break; case 1 : doUpdate(conn); break; case 2 : doDelete(conn); break; default :{ commit(conn); checkReplItems(conn); } } Thread.sleep(this.sleep); } catch (SQLException ex) { } finally { if (this.compareCount == this.compareNmax && this.compareNmax > 0) { this.compareCount = 1; checkConsistency(conn, "repltest", 20000L); checkConsistency(conn, "repltest2", 0L); } else this.compareCount++; } } } catch (Throwable ex) { ex.printStackTrace(); } finally { try { if (conn != null) { conn.setAutoCommit(true); this.dbPool.release(conn); } } catch (Throwable ex) { ex.printStackTrace(); } } } /** * Example code. * <p /> * <tt>java -Djava.util.logging.config.file=testlog.properties org.xmlBlaster.contrib.dbwatcher.Example -alertScheduler.pollInterval 10000 -db.password secret</tt> * @param args Command line */ public static void main(String[] args) { try { System.setProperty("java.util.logging.config.file", "testlog.properties"); LogManager.getLogManager().readConfiguration(); Preferences prefs = loadArgs(args); if (prefs == null) return; Info info = new Info(prefs); DbWatcherTester example = new DbWatcherTester(info); example.run(); } catch (Throwable e) { System.err.println("SEVERE: " + e.toString()); e.printStackTrace(); } } /** * Parse command line arguments * @param args Command line * @return Configuration */ public static Preferences loadArgs(String[] args) { try { Preferences prefs = Preferences.userRoot(); prefs.clear(); // String dbUrl = System.getProperty("db.url", "jdbc:oracle:thin:@localhost:1521:orcl"); // String dbUser = System.getProperty("db.user", "system"); String driverClass = System.getProperty("jdbc.drivers", "org.hsqldb.jdbcDriver:oracle.jdbc.driver.OracleDriver:com.microsoft.jdbc.sqlserver.SQLServerDriver:org.postgresql.Driver"); String dbUrl = System.getProperty("db.url", "jdbc:postgresql:test//localhost/test"); String dbUser = System.getProperty("db.user", "postgres"); String dbPassword = System.getProperty("db.password", ""); prefs.put("jdbc.drivers", driverClass); prefs.put("db.url", dbUrl); prefs.put("db.user", dbUser); prefs.put("db.password", dbPassword); prefs.put("stress.nmax", System.getProperty("stress.nmax", "1000")); prefs.put("stress.sleep", System.getProperty("stress.sleep", "0")); prefs.put("stress.compareNmax", System.getProperty("stress.compareNmax", "0")); prefs.put("stress.seed", System.getProperty("stress.seed", "0")); prefs.put("stress.commitCheck", System.getProperty("stress.commitCheck", "true")); for (int i=0; i<args.length; i++) { if (args[i].startsWith("-h")) { usage(); return null; } if (i < (args.length-1)) { if (args[i].startsWith("-")) { prefs.put(args[i].substring(1), args[++i]); } } } prefs.flush(); return prefs; } catch (Throwable e) { e.printStackTrace(); log.severe("Problems: " + e.toString()); } return null; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -