20 import java.sql.Connection;
21 import java.sql.PreparedStatement;
22 import java.sql.ResultSet;
23 import java.sql.SQLException;
24 import java.sql.Statement;
26 import java.util.Arrays;
27 import java.util.HashSet;
30 import testsuite.clusterj.AbstractClusterJModelTest;
34 private static final int NUMBER_OF_INSTANCES = 10;
36 private static final Set<Integer> expecteds =
new HashSet<Integer>();
38 for (
int i = 0;
i < NUMBER_OF_INSTANCES;++
i) {
52 public void testInsertBatch() {
54 connection.setAutoCommit(
false);
55 deleteAll(connection);
56 int[] counts = insertBatch(connection);
57 if (
getDebug()) System.out.println(Arrays.toString(counts));
58 for (
int i = 0;
i < NUMBER_OF_INSTANCES; ++
i) {
59 int count = counts[
i];
60 errorIfNotEqual(
"test executeBatch failure for " +
i, 1, count);
63 selectAndVerifyAll(connection);
65 }
catch (SQLException e) {
66 error(
"insert id, name, age, magic into t_basic values(?, ?, ?, ?) threw " + e.getMessage());
68 throw new RuntimeException(
"insert id, name, age, magic into t_basic values(?, ?, ?, ?)", e);
69 }
catch (RuntimeException e) {
75 public void testInsertBatchAutocommit() {
77 connection.setAutoCommit(
false);
78 deleteAll(connection);
79 connection.setAutoCommit(
true);
80 insertBatch(connection);
82 }
catch (SQLException e) {
83 error(
"insert id, name, age, magic into t_basic values(?, ?, ?, ?) threw " + e.getMessage());
85 throw new RuntimeException(
"insert id, name, age, magic into t_basic values(?, ?, ?, ?)", e);
86 }
catch (RuntimeException e) {
92 private int deleteAll(
Connection connection)
throws SQLException {
93 Statement deleteStatement = connection.createStatement();
94 int result = deleteStatement.executeUpdate(
"delete from t_basic");
95 deleteStatement.close();
100 private int[] insertBatch(
Connection connection) {
104 statement = connection.prepareStatement(
105 "insert into t_basic (id, name, age, magic) values(?, ?, ?, ?)");
106 for (
int i = 0;
i < NUMBER_OF_INSTANCES; ++
i) {
107 statement.setInt(1,
i);
108 statement.setString(2,
"Employee " +
i);
109 statement.setInt(3,
i);
110 statement.setInt(4,
i);
111 statement.addBatch();
113 counts = statement.executeBatch();
116 }
catch (SQLException e) {
117 throw new RuntimeException(
"insertBatch.executeBatch threw " + e.getMessage(), e);
122 private void selectAndVerifyAll(
Connection connection)
throws SQLException {
123 PreparedStatement selectStatement = connection.prepareStatement(
124 "select id, name, age, magic from t_basic");
125 ResultSet rs = selectStatement.executeQuery();
128 int id = rs.getInt(1);
129 verifyEmployee(rs,
id);
132 errorIfNotEqual(
"Wrong number of instances in database.", expecteds, actuals);
135 private void verifyEmployee(ResultSet rs,
int id) {
138 errorIfNotEqual(
"Verify name id: " +
id,
"Employee " +
id, name);
139 int age = rs.getInt(3);
140 errorIfNotEqual(
"Verify age id: " +
id,
id, age);
141 int magic = rs.getInt(4);
142 errorIfNotEqual(
"Verify magic id: " +
id,
id, magic);
143 }
catch (SQLException e) {
144 if (
getDebug()) e.printStackTrace();