MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BatchTest.java
1 /*
2  Copyright 2011, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 package jdbctest;
19 
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;
25 
26 import java.util.Arrays;
27 import java.util.HashSet;
28 import java.util.Set;
29 
30 import testsuite.clusterj.AbstractClusterJModelTest;
31 
32 public class BatchTest extends AbstractClusterJModelTest {
33 
34  private static final int NUMBER_OF_INSTANCES = 10;
35 
36  private static final Set<Integer> expecteds = new HashSet<Integer>();
37  static {
38  for (int i = 0; i < NUMBER_OF_INSTANCES;++i) {
39  expecteds.add(i);
40  }
41  }
42  @Override
43  public boolean getDebug() {
44  return false;
45  }
46 
47  @Override
48  public void localSetUp() {
49  super.localSetUp();
50  }
51 
52  public void testInsertBatch() {
53  try {
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);
61  }
62  connection.commit();
63  selectAndVerifyAll(connection);
65  } catch (SQLException e) {
66  error("insert id, name, age, magic into t_basic values(?, ?, ?, ?) threw " + e.getMessage());
67  if (getDebug()) e.printStackTrace();
68  throw new RuntimeException("insert id, name, age, magic into t_basic values(?, ?, ?, ?)", e);
69  } catch (RuntimeException e) {
70  if (getDebug()) e.printStackTrace();
71  }
72  failOnError();
73  }
74 
75  public void testInsertBatchAutocommit() {
76  try {
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());
84  if (getDebug()) e.printStackTrace();
85  throw new RuntimeException("insert id, name, age, magic into t_basic values(?, ?, ?, ?)", e);
86  } catch (RuntimeException e) {
87  if (getDebug()) e.printStackTrace();
88  }
89  failOnError();
90  }
91 
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();
96  connection.commit();
97  return result;
98  }
99 
100  private int[] insertBatch(Connection connection) {
101  PreparedStatement statement;
102  int[] counts = null;
103  try {
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();
112  }
113  counts = statement.executeBatch();
114  statement.close();
115  return counts;
116  } catch (SQLException e) {
117  throw new RuntimeException("insertBatch.executeBatch threw " + e.getMessage(), e);
118  }
119 
120  }
121 
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();
126  Set<Integer> actuals = new HashSet<Integer>();
127  while (rs.next()) {
128  int id = rs.getInt(1);
129  verifyEmployee(rs, id);
130  actuals.add(id);
131  }
132  errorIfNotEqual("Wrong number of instances in database.", expecteds, actuals);
133  }
134 
135  private void verifyEmployee(ResultSet rs, int id) {
136  try {
137  String name = rs.getString(2);
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();
145  }
146  }
147 
148 }