File: LockTableTest.java

package info (click to toggle)
derby 10.14.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 79,056 kB
  • sloc: java: 691,961; sql: 42,686; xml: 20,512; sh: 3,373; sed: 96; makefile: 60
file content (399 lines) | stat: -rw-r--r-- 14,714 bytes parent folder | download | duplicates (4)
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*

 Derby - Class org.apache.derbyTesting.functionTests.tests.lang.LockTableTest

 Licensed to the Apache Software Foundation (ASF) under one or more
 contributor license agreements.  See the NOTICE file distributed with
 this work for additional information regarding copyright ownership.
 The ASF licenses this file to You under the Apache License, Version 2.0
 (the "License"); you may not use this file except in compliance with
 the License.  You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

 */
// Note: This test could be refined by modifying the BaseJDBCTestCase
//       method assertStatementError(new String[],Statement,String)
//       and all methods down that chain to search for the variable
//       values in the SQL error messages as well, in this case, in this
//       case, to check for 'exclusive' or 'share' in error X0202.

package org.apache.derbyTesting.functionTests.tests.lang;

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

import junit.framework.Test;

import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.CleanDatabaseTestSetup;
import org.apache.derbyTesting.junit.DatabasePropertyTestSetup;
import org.apache.derbyTesting.junit.JDBC;
import org.apache.derbyTesting.junit.TestConfiguration;

/**
 * Tests the LOCK TABLE in various modes.
 */
public class LockTableTest extends BaseJDBCTestCase {

    public LockTableTest(String name) {
        super(name);
    }

    /**
     * Construct top level suite in this JUnit test
     * The suite is wrapped in a DatabasePropertyTestSetup to
     * lower the locking times.
     *
     * @return A suite containing embedded fixtures
     */
    public static Test suite() {
        Properties properties = new Properties();
        properties.setProperty("derby.storage.rowLocking", "false");
        properties.setProperty("derby.locks.waitTimeout", "7");
        properties.setProperty("derby.locks.deadlockTimeout", "5");

        Test suite = TestConfiguration.embeddedSuite (LockTableTest.class);
        suite = new DatabasePropertyTestSetup(suite, properties, true);
        return new CleanDatabaseTestSetup(suite) {
            /**
             * Creates the schemas and table used in the test cases.
             *
             * @throws SQLException
             */
            protected void decorateSQL(Statement s) throws SQLException {
                Connection conn = getConnection();
                conn.setAutoCommit(false);
                s.executeUpdate("create schema u1");
                s.executeUpdate("create schema u2");
                conn.commit();
            }
        };
    }

    protected void setUp() throws Exception {
        super.setUp();
    }

    /**
     * Tear-down the fixture by removing the tables and schemas
     * @throws Exception
     */
    protected void tearDown() throws Exception {
        // first drop t2 only - it is only used in one fixture.
        // if doing this in the other block, the schemas might not
        // get dropped.
        Statement s = createStatement();
/*        try {
            s.executeUpdate("drop table u1.t2");
        } catch(SQLException sqe) {
            if (!(sqe.getSQLState().equalsIgnoreCase("42X05") 
                    || sqe.getSQLState().equalsIgnoreCase("42Y55")))
            {
                fail("oops in teardown, encountered some other error than " +
                		"'object does not exist' or " +
                		"'cannot drop object because it doesn't exist'");
                sqe.printStackTrace();
            }
        }
        finally {
            s.close();
        }*/
        try {
            s = createStatement();
            s.executeUpdate("drop table u1.t1");
            s.executeUpdate("drop schema u1 restrict");
            s.executeUpdate("drop schema u2 restrict");
        } catch(SQLException sqe) {
            if (!(sqe.getSQLState().equalsIgnoreCase("42X05") 
                    || sqe.getSQLState().equalsIgnoreCase("42Y55")))
            {
                fail("oops in teardown, encountered some other error than " +
                		"'object does not exist' or " +
                		"'cannot drop object because it doesn't exist'");
                sqe.printStackTrace();
            }
        }
        finally {
            s.close();
        }
        super.tearDown();
    }

    /** sets up the connection for a user
     * 
     * @return A connection with autocommit set to false
     * @exception SQLException
     */
    private Connection setConnection(String userString) throws SQLException {
        Connection c1 = openUserConnection(userString);
        c1.setAutoCommit(false);
        return c1;
    }

    /* create table t1, used in most of the fixtures
     * 
     * @exception SQLException
     */
    private void setupTable(Statement s) throws SQLException {
        s.executeUpdate("create table t1(c1 int)");
        s.executeUpdate("insert into t1 values 1");
    }

    /* get the query to get the locks
     * 
     * @return String with the query
     */
    public static String getSelectLocksString() {
        String sql = 
                "select " + 
                        "cast(username as char(8)) as username, " +
                        "cast(t.type as char(15)) as trantype, " +
                        "cast(l.type as char(8)) as type, " +
                        "cast(lockcount as char(3)) as cnt, " +
                        "mode, " +
                        "cast(tablename as char(12)) as tabname, " +
                        "cast(lockname as char(10)) as lockname, " +
                        "state, " +
                        "status " +
                        "from syscs_diag.lock_table l " +
                        "right outer join syscs_diag.transaction_table t " +
                        "on l.xid = t.xid where l.tableType <> 'S' " +
                        "order by " +
                        "tabname, type desc, mode, cnt, lockname";
        return sql;
    }
    
    /**
     * Tests that LOCK TABLE is not allowed on system tables.
     * 
     * @exception SQLException
     */
    public void testSystemTable() throws SQLException {
        Statement s = createStatement();
        assertStatementError("42X62", s,
                "lock table sys.systables in share mode");
        s.close();
    }

    /**
     * Tests LOCK TABLE command - exclusive vs exclusive mode
     * 
     * @exception SQLException
     */
    public void testTXvsTXLocks() throws SQLException {
        //set up the connections;
        Connection c1 = setConnection("U1");
        Statement s1 = c1.createStatement();
        Connection c2 = setConnection("U2");
        Statement s2 = c2.createStatement();

        setupTable(s1);
        c1.commit();

        s1.executeUpdate("lock table u1.t1 in exclusive mode");
        // We expect X0X02 (Table cannot be locked 'EXCLUSIVE' mode) 
        // and 40XL1 (A lock could not be obtained within the time requested).
        assertStatementError(new String[] {"X0X02","40XL1"},s2,
                "lock table u1.t1 in exclusive mode");
        // verify we still have the lock
        ResultSet rs = s1.executeQuery(getSelectLocksString());
        JDBC.assertFullResultSet(rs, new String[][]{
                {"U1", "UserTransaction", "TABLE", "1",
                    "X", "T1", "Tablelock", "GRANT", "ACTIVE"}
        });
        // verify user 1 can insert into the table
        s1.executeUpdate("insert into t1 values 2");
        rs = s1.executeQuery("select count(*) from t1");
        JDBC.assertSingleValueResultSet(rs, "2");
        // But user 2 should not be able to insert
        assertStatementError("40XL1", s2, "insert into u1.t1 values 9");
        rs = s1.executeQuery("select count(*) from t1");
        JDBC.assertSingleValueResultSet(rs, "2");
        // but select should be ok
        rs = s1.executeQuery("select count(*) from u1.t1");
        JDBC.assertSingleValueResultSet(rs, "2");
        rs.close();
        c1.commit();
        s1.executeUpdate("drop table U1.t1");
        c1.commit();
        s1.close();
        s2.close();
        c1.close();
        c2.rollback();
        c2.close();
    }

    /**
     * Tests LOCK TABLE command - exclusive vs shared mode
     * 
     * @exception SQLException
     */
    public void testTXvsTSLocks() throws SQLException {
        Connection c1 = setConnection("U1");
        Statement s1 = c1.createStatement();
        Connection c2 = setConnection("U2");
        Statement s2 = c2.createStatement();

        setupTable(s1);
        c1.commit();

        // - test TX vs TS locks
        s1.executeUpdate("lock table t1 in exclusive mode");
        // We expect X0X02 (Table cannot be locked in 'SHARE' mode) 
        // and 40XL1 (A lock could not be obtained within the time requested).
        assertStatementError(new String[] {"X0X02","40XL1"},s2,
                "lock table u1.t1 in share mode");
        // verify we still have the lock
        ResultSet rs = s1.executeQuery(getSelectLocksString());
        JDBC.assertFullResultSet(rs, new String[][]{
                {"U1", "UserTransaction", "TABLE", "1",
                    "X", "T1", "Tablelock", "GRANT", "ACTIVE"}
        });
        // verify we can still insert into the table
        s1.executeUpdate("insert into t1 values 3");
        rs = s1.executeQuery("select count(*) from t1");
        JDBC.assertSingleValueResultSet(rs, "2");
        s1.executeUpdate("drop table U1.t1");
        c1.commit();
        s1.close();
        s2.close();
        c1.close();
        c2.rollback();
        c2.close();
    }

    /**
     * Tests LOCK TABLE command - shared vs exclusive mode
     * 
     * @exception SQLException
     */
    public void testTSvsTXLocks() throws SQLException {
        Connection c1 = setConnection("U1");
        Statement s1 = c1.createStatement();
        Connection c2 = setConnection("U2");
        Statement s2 = c2.createStatement();

        setupTable(s1);
        c1.commit();

        // -- test TS vs TX locks
        s1.executeUpdate("lock table t1 in share mode");
        // We expect X0X02 (Table cannot be locked in 'EXLUSIVE' mode) 
        // and 40XL1 (A lock could not be obtained within the time requested).
        assertStatementError(new String[] {"X0X02","40XL1"},s2,
                "lock table u1.t1 in exclusive mode");
        // verify we still have the lock
        ResultSet rs = s1.executeQuery(getSelectLocksString());
        JDBC.assertFullResultSet(rs, new String[][]{
                {"U1", "UserTransaction", "TABLE", "1",
                    "S", "T1", "Tablelock", "GRANT", "ACTIVE"}
        });
        // verify insert
        s1.executeUpdate("insert into t1 values 4");
        rs = s1.executeQuery("select count(*) from t1");
        JDBC.assertSingleValueResultSet(rs, "2");
        s1.executeUpdate("drop table U1.t1");
        c1.commit();
        s1.close();
        s2.close();
        c1.close();
        c2.rollback();
        c2.close();
    }

    /**
     * Tests LOCK TABLE command - shared vs shared mode
     * 
     * @exception SQLException
     */
    public void testTSvsTSLocks() throws SQLException {
        Connection c1 = setConnection("U1");
        Statement s1 = c1.createStatement();
        Connection c2 = setConnection("U2");
        Statement s2 = c2.createStatement();

        setupTable(s1);
        c1.commit();

        // -- test TS vs TS locks
        s1.executeUpdate("lock table t1 in share mode");
        // expect success on lock, but now user 1 may not update.
        assertUpdateCount(s2, 0, "lock table u1.t1 in share mode");
        // verify we have two locks
        ResultSet rs = s1.executeQuery(getSelectLocksString());
        JDBC.assertFullResultSet(rs, new String[][]{
                {"U2", "UserTransaction", "TABLE", "1",
                    "S", "T1", "Tablelock", "GRANT", "ACTIVE"},
                {"U1", "UserTransaction", "TABLE", "1",
                    "S", "T1", "Tablelock", "GRANT", "ACTIVE"}
        });
        // verify that with a share lock for user 2 place, user 1 cannot insert
        assertStatementError("40XL1", s1, "insert into t1 values 5");
        rs = s1.executeQuery("select count(*) from t1");
        JDBC.assertSingleValueResultSet(rs, "1");
        c2.rollback();
        c1.rollback();
        s1.executeUpdate("drop table U1.t1");
        c1.commit();
        s1.close();
        s2.close();
        c1.close();
        c2.close();
    }

    /**
     * test with rollback.
     * 
     * @exception SQLException
     */
    public void testWithRolledBack() throws SQLException {
        Connection c1 = setConnection("U1");
        Statement s1 = c1.createStatement();
        Connection c2 = setConnection("U2");
        Statement s2 = c2.createStatement();

        setupTable(s1);
        c1.commit();

        // -- create another table
        s1.executeUpdate("create table t2(c1 int)");
        c1.commit();

        // verify that the user getting error on lock table
        // doesn't get rolled back, so other locks remain in  place.
        s1.executeUpdate("lock table t1 in share mode");
        s2.executeUpdate("lock table u1.t2 in share mode");
        // Attempt to lock t1 in exclusive mode, while it has been share-locked.
        // We expect X0X02 (Table cannot be locked in 'EXCLUSIVE' mode) 
        // and 40XL1 (A lock could not be obtained within the time requested).
        assertStatementError(new String[] {"X0X02","40XL1"},s2,
                "lock table u1.t1 in exclusive mode");
        // verify the other user still has the lock
        ResultSet rs = s1.executeQuery(getSelectLocksString());
        JDBC.assertFullResultSet(rs, new String[][]{
                {"U1", "UserTransaction", "TABLE", "1",
                    "S", "T1", "Tablelock", "GRANT", "ACTIVE"},
                {"U2", "UserTransaction", "TABLE", "1",
                    "S", "T2", "Tablelock", "GRANT", "ACTIVE"}
        });
        c2.rollback();
        c1.rollback();
        s1.executeUpdate("drop table U1.t2");
        s1.executeUpdate("drop table U1.t1");
        c1.commit();
        s1.close();
        s2.close();
        c1.close();
        c2.close();
    }
}