Skip to main content
summaryrefslogtreecommitdiffstats
blob: d0af76f7698fe7bb7fe12e6c1012c4df70a2afe4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
 * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.emf.cdo.tests.db.capabilities;

import org.eclipse.net4j.db.DBUtil;
import org.eclipse.net4j.db.IDBConnectionProvider;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import junit.framework.TestCase;

/**
 * This is a simple test case that can be used to analyze how a DBMS handles DML in the middle of a transaction.
 * 
 * @author Stefan Winkler
 */
public abstract class AbstractCapabilityTest extends TestCase
{
  public AbstractCapabilityTest(String name)
  {
    super(name);
  }

  public void testDirtyRead() throws Exception
  {
    msg("TEST " + getClass().getSimpleName() + " - DIRTY READ");
    msg("----------------------------------------------------------");
    msg("Transaction 1 changes a value, transaction 2 will read the value.");
    msg("The value of transaction 2 should be UNCHANGED (else we have a dirty read)");

    Thread t = new Thread()
    {
      @Override
      public void run()
      {
        try
        {
          IDBConnectionProvider provider = getConnectionProvider();
          Connection transaction1 = provider.getConnection();
          transaction1.setAutoCommit(false);

          Statement tx1stmt = transaction1.createStatement();
          tx1stmt.executeUpdate("update status_table set status = 'changed' where trans = 'transaction1'");
          msg("Read value (transaction 1) is "
              + select(transaction1, "select status from status_table where trans = 'transaction1'").toUpperCase());
          sleep(1000);

          transaction1.rollback();
          transaction1.close();
        }
        catch (Exception e)
        {
          throw new Error(e);
        }
      }
    };

    t.start();
    Thread.sleep(300);

    IDBConnectionProvider provider = getConnectionProvider();
    Connection transaction2 = provider.getConnection();

    transaction2.setAutoCommit(false);

    msg("Read value (transaction 2) is "
        + select(transaction2, "select status from status_table where trans = 'transaction1'").toUpperCase());
    msg("----------------------------------------------------------");
    transaction2.rollback();
    transaction2.close();
  }

  public void testRollback() throws Exception
  {
    msg("TEST " + getClass().getSimpleName() + " - ROLLBACK");
    msg("----------------------------------------------------------");
    msg("Transaction changes a value and does a rollback.");
    msg("The value of after rollback should be UNCHANGED.");

    IDBConnectionProvider provider = getConnectionProvider();
    Connection transaction1 = provider.getConnection();
    transaction1.setAutoCommit(false);

    Statement tx1stmt = transaction1.createStatement();

    try
    {
      tx1stmt.executeUpdate("update status_table set status = 'changed' where trans = 'transaction1'");
    }
    finally
    {
      DBUtil.close(tx1stmt);
    }

    msg("Read value before rollback is "
        + select(transaction1, "select status from status_table where trans = 'transaction1'").toUpperCase());

    transaction1.rollback();
    transaction1.close();

    Connection view = provider.getConnection();
    msg("Read value after rollback is "
        + select(view, "select status from status_table where trans = 'transaction1'").toUpperCase());
    view.close();
    msg("----------------------------------------------------------");
  }

  public void testDml() throws Exception
  {
    msg("TEST " + getClass().getSimpleName() + " - DML");
    msg("----------------------------------------------------------");
    msg("Transaction 1 will execute DML, transaction 2 will just change its data.");

    Thread t = new Thread()
    {
      @Override
      public void run()
      {
        try
        {
          IDBConnectionProvider provider = getConnectionProvider();
          Connection transaction2 = provider.getConnection();
          Statement tx2stmt = transaction2.createStatement();
          transaction2.setAutoCommit(false);
          tx2stmt.executeUpdate("update status_table set status = 'changed' where trans = 'transaction2'");
          tx2stmt.executeUpdate("update change_table set status = 'changed' where trans = 'transaction2'");
          tx2stmt.close();
          sleep(1000);
          transaction2.rollback();
          transaction2.close();
        }
        catch (Exception e)
        {
          throw new Error(e);
        }
      }
    };

    t.start();
    Thread.sleep(100);

    IDBConnectionProvider provider = getConnectionProvider();
    Connection transaction1 = provider.getConnection();
    transaction1.setAutoCommit(false);

    Statement tx1stmt = transaction1.createStatement();

    tx1stmt.executeUpdate("update status_table set status = 'changed' where trans = 'transaction1'");
    tx1stmt.executeUpdate("update change_table set status = 'changed' where trans = 'transaction1'");

    tx1stmt.execute("alter table change_table add new_column varchar(255) default 'added column present'");

    tx1stmt.close();

    transaction1.rollback();
    transaction1.close();

    t.join();

    Connection view = provider.getConnection();

    msg("transaction1: unchanged table record is "
        + select(view, "select status from status_table where trans = 'transaction1'").toUpperCase());
    msg("transaction2: unchanged table record is "
        + select(view, "select status from status_table where trans = 'transaction2'").toUpperCase());

    msg("transaction1: changed table record is "
        + select(view, "select status from change_table where trans = 'transaction1'").toUpperCase());
    msg("transaction2: changed table record is "
        + select(view, "select status from change_table where trans = 'transaction2'").toUpperCase());

    String present = "present";
    try
    {
      select(view, "select new_column from change_table where trans = 'transaction2'");
    }
    catch (SQLException e)
    {
      present = "not present";
    }

    msg("Added column is " + present.toUpperCase());
    view.close();

    msg("----------------------------------------------------------");
  }

  @Override
  protected void setUp() throws Exception
  {
    // create table
    Connection conn = getConnectionProvider().getConnection();
    conn.setAutoCommit(false);
    Statement stmt = conn.createStatement();

    // make sure tables don't exist!
    try
    {
      stmt.execute("drop table status_table");
    }
    catch (Exception e)
    {
    }

    try
    {
      stmt.execute("drop table change_table");
    }
    catch (Exception e)
    {
    }

    stmt.execute("create table status_table (trans varchar(255), status varchar(255))");
    stmt.execute("insert into status_table values ('transaction1', 'unchanged')");
    stmt.execute("insert into status_table values ('transaction2', 'unchanged')");

    stmt.execute("create table change_table (trans varchar(255), status varchar(255))");
    stmt.execute("insert into change_table values ('transaction1', 'unchanged')");
    stmt.execute("insert into change_table values ('transaction2', 'unchanged')");

    conn.commit();
    stmt.close();
    conn.close();
  }

  @Override
  protected void tearDown() throws Exception
  {
    Connection conn = getConnectionProvider().getConnection();
    conn.setAutoCommit(true);
    Statement stmt = conn.createStatement();

    stmt.execute("drop table status_table");
    stmt.execute("drop table change_table");

    stmt.close();
    conn.close();
  }

  protected abstract IDBConnectionProvider getConnectionProvider();

  private void msg(String string)
  {
    System.out.println(string);
  }

  private String select(Connection conn, String sql) throws SQLException
  {
    ResultSet rs = null;
    try
    {
      rs = conn.createStatement().executeQuery(sql);
      rs.next();
      return rs.getString(1);
    }
    finally
    {
      if (rs != null)
      {
        try
        {
          rs.close();
        }
        catch (SQLException ex)
        {
          // NOP
        }
      }
    }
  }

  @SuppressWarnings("unused")
  private void sqlDump(Connection conn, String sql)
  {
    ResultSet rs = null;
    try
    {
      System.out.format("Dumping output of %s\n", sql); //$NON-NLS-1$
      rs = conn.createStatement().executeQuery(sql);
      int numCol = rs.getMetaData().getColumnCount();

      StringBuilder row = new StringBuilder(" ");
      for (int c = 1; c <= numCol; c++)
      {
        row.append(String.format("%15s | ", rs.getMetaData().getColumnLabel(c))); //$NON-NLS-1$
      }

      System.out.println(row.toString());

      row = new StringBuilder();
      for (int c = 1; c <= numCol; c++)
      {
        row.append("-----------------+"); //$NON-NLS-1$
      }

      System.out.println(row.toString());

      while (rs.next())
      {
        row = new StringBuilder(" ");
        for (int c = 1; c <= numCol; c++)
        {
          row.append(String.format("%15s | ", rs.getString(c))); //$NON-NLS-1$
        }

        System.out.println(row.toString());
      }

      row = new StringBuilder();
      for (int c = 1; c <= numCol; c++)
      {
        row.append("-----------------+"); //$NON-NLS-1$
      }

      System.out.println(row.toString());
    }
    catch (SQLException ex)
    {
      // NOP
    }
    finally
    {
      if (rs != null)
      {
        try
        {
          rs.close();
        }
        catch (SQLException ex)
        {
          // NOP
        }
      }
    }
  }
}

Back to the top