1 30 31 32 package org.hsqldb.test; 33 34 import java.sql.Connection ; 35 import java.sql.DriverManager ; 36 import java.sql.PreparedStatement ; 37 import java.sql.SQLException ; 38 import java.sql.Statement ; 39 import java.sql.Timestamp ; 40 41 public class TestBatchBug { 42 43 static final int DATASET_COUNT = 2; 44 static final int DECIMAL_FIELDS_PER_DATASET = 2; 45 static final String TABLE_ATTR_CACHED = "CACHED"; 46 static final String IN_PROCESS_FILE_URL = 47 "jdbc:hsqldb:/temp/hsqldb/perftest"; 48 static final String HSQLDB_LOCALHOST_URL = 49 "jdbc:hsqldb:hsql://localhost/yourtest"; 50 ; 51 static final String TEST_TABLE_NAME = "CSBug"; 52 static String FIELD_LIST_WITHOUT_ID = "Kennung, Last_Update "; 53 54 static String FIELD_LIST_WITH_ID = "ID, "; 56 static String SQL_SELECT_ALL_FIELDS = "SELECT "; 57 58 static { 60 for (int i = 1; i <= DECIMAL_FIELDS_PER_DATASET; i++) { 61 FIELD_LIST_WITHOUT_ID += ", Field_" + i; 62 } 63 64 FIELD_LIST_WITH_ID += FIELD_LIST_WITHOUT_ID; 65 SQL_SELECT_ALL_FIELDS += FIELD_LIST_WITH_ID + " FROM " 66 + TEST_TABLE_NAME; 67 } 68 69 static int ldfNrFuerKennung; 70 71 public static void main(String [] arg) { 72 73 try { 74 75 Class.forName("org.hsqldb.jdbcDriver"); 77 78 String [] urls = { 79 IN_PROCESS_FILE_URL, HSQLDB_LOCALHOST_URL, 80 }; 81 82 for (int i = 0; i < urls.length; i++) { 83 String url = urls[i]; 84 String [] tableAttrs = { TABLE_ATTR_CACHED }; 85 86 for (int iAttr = 0; iAttr < tableAttrs.length; iAttr++) { 87 testURL(url, "CACHED"); 88 } 89 } 90 91 System.out.println("bye"); 92 } catch (Exception e) { 93 e.printStackTrace(); 94 } 95 } 96 97 static void testURL(String url, String tableAttr) throws SQLException { 98 99 System.out.println(url); 100 101 Connection con = DriverManager.getConnection(url, "sa", ""); 102 103 reCreateTable(con, "CACHED"); 104 populateTable(con); 105 con.close(); 106 } 107 108 static void reCreateTable(Connection con, 109 String tableAttr) throws SQLException { 110 111 String cvsFileName = TEST_TABLE_NAME + ".csv"; 112 113 Statement stmt = con.createStatement(); 115 116 try { 118 119 stmt.executeUpdate("DROP TABLE " + TEST_TABLE_NAME); 121 } catch (SQLException e) { System.out.println(e.getMessage()); 123 } 124 125 StringBuffer sql = new StringBuffer (); 126 127 sql.append("CREATE "); 128 sql.append(tableAttr); sql.append(" TABLE "); 130 sql.append(TEST_TABLE_NAME); 131 sql.append(" ("); 132 sql.append("Id integer IDENTITY"); 133 sql.append(", "); 134 sql.append("Kennung varchar(20) NOT NULL"); 135 136 sql.append(", last_update TIMESTAMP "); 138 sql.append("DEFAULT CURRENT_TIMESTAMP NOT NULL"); 139 140 for (int i = 1; i <= DECIMAL_FIELDS_PER_DATASET; i++) { 141 sql.append(", Field_" + i + " decimal"); 142 } 143 144 sql.append(", UNIQUE(Kennung)"); 145 sql.append(")"); 146 System.out.println(sql.toString()); 147 stmt.executeUpdate(sql.toString()); 148 149 sql = new StringBuffer (); 151 152 sql.append("DELETE FROM "); 153 sql.append(TEST_TABLE_NAME); 154 System.out.println(sql.toString()); 155 stmt.executeUpdate(sql.toString()); 156 stmt.close(); } 158 159 static void populateTable(Connection con) throws SQLException { 161 162 long startTime = System.currentTimeMillis(); 163 Timestamp now = new Timestamp (startTime); 164 165 con.setAutoCommit(false); 166 167 String sql = createInsertSQL(true, false); 168 PreparedStatement prep = con.prepareStatement(sql); 169 170 prep.clearParameters(); 171 prep.setString(1, "xxx"); 172 prep.setTimestamp(2, now); 174 for (int ii = 0; ii < DECIMAL_FIELDS_PER_DATASET; ii++) { 175 prep.setDouble(ii + 3, 0.123456789); } 177 178 prep.addBatch(); 179 prep.setString(1, "yyy"); 180 prep.setTimestamp(2, now); 182 for (int ii = 0; ii < DECIMAL_FIELDS_PER_DATASET; ii++) { 183 prep.setDouble(ii + 3, 0.123456789); } 185 186 prep.addBatch(); 187 188 int[] updateCounts = prep.executeBatch(); 189 190 con.setAutoCommit(true); 191 prep.close(); 192 } 193 194 static String createInsertSQL(boolean prepStmt, 195 boolean getIdAfterInsert) { 196 197 StringBuffer sql = new StringBuffer (); 198 199 sql.append("INSERT INTO "); 200 sql.append(TEST_TABLE_NAME); 201 sql.append(" ("); 202 sql.append(FIELD_LIST_WITHOUT_ID); 203 sql.append(") VALUES ("); 204 205 Timestamp now = new Timestamp (System.currentTimeMillis()); 206 Object val = "?"; 207 208 if (prepStmt) { 209 sql.append(val + ", " + val); 210 } else { 211 long millis = System.currentTimeMillis(); 212 213 sql.append("'Ken"); 214 sql.append((++ldfNrFuerKennung) + "'"); 215 216 val = new Double (0.123456789) + ""; 217 218 sql.append(", '" + now.toString() + "'"); 219 } 220 221 for (int i = 1; i <= DECIMAL_FIELDS_PER_DATASET; i++) { 222 sql.append(", " + val); 223 } 224 225 sql.append(")"); 226 227 String ret = sql.toString(); 228 229 System.out.println(ret); 230 231 return ret; 232 } 233 } 234 | Popular Tags |