KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > test > TestBatchBug


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb.test;
33
34 import java.sql.Connection JavaDoc;
35 import java.sql.DriverManager JavaDoc;
36 import java.sql.PreparedStatement JavaDoc;
37 import java.sql.SQLException JavaDoc;
38 import java.sql.Statement JavaDoc;
39 import java.sql.Timestamp JavaDoc;
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 JavaDoc TABLE_ATTR_CACHED = "CACHED";
46     static final String JavaDoc IN_PROCESS_FILE_URL =
47         "jdbc:hsqldb:/temp/hsqldb/perftest";
48     static final String JavaDoc HSQLDB_LOCALHOST_URL =
49         "jdbc:hsqldb:hsql://localhost/yourtest";
50     ;
51     static final String JavaDoc TEST_TABLE_NAME = "CSBug";
52     static String JavaDoc FIELD_LIST_WITHOUT_ID = "Kennung, Last_Update ";
53
54 // wird in static {} erweitert:
55
static String JavaDoc FIELD_LIST_WITH_ID = "ID, ";
56     static String JavaDoc SQL_SELECT_ALL_FIELDS = "SELECT ";
57
58 // wird in static {} erweitert:
59
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 JavaDoc[] arg) {
72
73         try {
74
75 // Load the HSQL Database Engine JDBC driver
76
Class.forName("org.hsqldb.jdbcDriver");
77
78             String JavaDoc[] urls = {
79                 IN_PROCESS_FILE_URL, HSQLDB_LOCALHOST_URL,
80             };
81
82             for (int i = 0; i < urls.length; i++) {
83                 String JavaDoc url = urls[i];
84                 String JavaDoc[] 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 JavaDoc e) {
93             e.printStackTrace();
94         }
95     }
96
97     static void testURL(String JavaDoc url, String JavaDoc tableAttr) throws SQLException JavaDoc {
98
99         System.out.println(url);
100
101         Connection JavaDoc con = DriverManager.getConnection(url, "sa", "");
102
103         reCreateTable(con, "CACHED");
104         populateTable(con);
105         con.close();
106     }
107
108     static void reCreateTable(Connection JavaDoc con,
109                               String JavaDoc tableAttr) throws SQLException JavaDoc {
110
111         String JavaDoc cvsFileName = TEST_TABLE_NAME + ".csv";
112
113 // Create a statement object
114
Statement JavaDoc stmt = con.createStatement();
115
116 // Try to drop the table
117
try {
118
119 // stmt.executeUpdate("DROP TABLE " + TEST_TABLE_NAME + " IF EXISTS");
120
stmt.executeUpdate("DROP TABLE " + TEST_TABLE_NAME);
121         } catch (SQLException JavaDoc e) { // Ignore Exception, because the table may not yet exist
122
System.out.println(e.getMessage());
123         }
124
125         StringBuffer JavaDoc sql = new StringBuffer JavaDoc();
126
127         sql.append("CREATE ");
128         sql.append(tableAttr); // z.B. "CACHED"
129
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 // etwas andere Schreibweise von CURRENT TIMESTAMP
137
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 // CLEAR TABLE
150
sql = new StringBuffer JavaDoc();
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(); // is no longer used
157
}
158
159 // tries to enter 2 records
160
static void populateTable(Connection JavaDoc con) throws SQLException JavaDoc {
161
162         long startTime = System.currentTimeMillis();
163         Timestamp JavaDoc now = new Timestamp JavaDoc(startTime);
164
165         con.setAutoCommit(false);
166
167         String JavaDoc sql = createInsertSQL(true, false);
168         PreparedStatement JavaDoc prep = con.prepareStatement(sql);
169
170         prep.clearParameters();
171         prep.setString(1, "xxx");
172         prep.setTimestamp(2, now); // last_update
173

174         for (int ii = 0; ii < DECIMAL_FIELDS_PER_DATASET; ii++) {
175             prep.setDouble(ii + 3, 0.123456789); // Wert
176
}
177
178         prep.addBatch();
179         prep.setString(1, "yyy");
180         prep.setTimestamp(2, now); // last_update
181

182         for (int ii = 0; ii < DECIMAL_FIELDS_PER_DATASET; ii++) {
183             prep.setDouble(ii + 3, 0.123456789); // Wert
184
}
185
186         prep.addBatch();
187
188         int[] updateCounts = prep.executeBatch();
189
190         con.setAutoCommit(true);
191         prep.close();
192     }
193
194     static String JavaDoc createInsertSQL(boolean prepStmt,
195                                   boolean getIdAfterInsert) {
196
197         StringBuffer JavaDoc sql = new StringBuffer JavaDoc();
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 JavaDoc now = new Timestamp JavaDoc(System.currentTimeMillis());
206         Object JavaDoc 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 JavaDoc(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 JavaDoc ret = sql.toString();
228
229         System.out.println(ret);
230
231         return ret;
232     }
233 }
234
Popular Tags