File: ReleaseCompileLocksTest.java

package info (click to toggle)
derby 10.14.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 78,896 kB
  • sloc: java: 691,930; sql: 42,686; xml: 20,511; sh: 3,373; sed: 96; makefile: 60
file content (282 lines) | stat: -rw-r--r-- 9,457 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
/*
Derby - Class org.apache.derbyTesting.functionTests.tests.lang.ReleaseCompileLocksTest

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.

*/

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

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import junit.framework.Test;
import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.CleanDatabaseTestSetup;
import org.apache.derbyTesting.junit.JDBC;
import org.apache.derbyTesting.junit.TestConfiguration;


/**
 * Tests for forupdate. 
 *
 */
public class ReleaseCompileLocksTest extends BaseJDBCTestCase {


	/* Public constructor required for running test as standalone JUnit. */    
	public ReleaseCompileLocksTest(String name) {
		super(name);
	}
    
    /**
     * Sets the auto commit to false.
     */
    protected void initializeConnection(Connection conn) throws SQLException {
        conn.setAutoCommit(false);
    }


	/* Set up fixture */ 
	protected void setUp() throws SQLException {
	    Statement stmt = createStatement();

	    stmt.execute("create function dmlstatic() returns INT " +
	    	"parameter style java language java external name " +
	    	"'org.apache.derbyTesting.functionTests.util.StaticInitializers." +
	    	"DMLInStaticInitializer.getANumber' no sql");
                  
	    stmt.execute("create function insertstatic() returns INT " +
	    	"parameter style java language java external name " +
	    	"'org.apache.derbyTesting.functionTests.util.StaticInitializers." +
	    	"InsertInStaticInitializer.getANumber' no sql");

        stmt.execute("CREATE PROCEDURE WAIT_FOR_POST_COMMIT() "
                + "LANGUAGE JAVA EXTERNAL NAME "
                + "'org.apache.derbyTesting.functionTests.util."
                + "T_Access.waitForPostCommitToFinish' "
                + "PARAMETER STYLE JAVA");

        stmt.close();
        commit();
	}



	/**
         * Create a suite of tests.
         **/
        public static Test suite() {
            BaseTestSuite suite =
                new BaseTestSuite("ReleasecompileLocksTest");

        	suite.addTest(baseSuite("ReleaseCompileLocksTest:embedded"));
        	suite.addTest(TestConfiguration.clientServerDecorator(baseSuite("ReleaseCompileLocksTest:client")));
        	return suite;
    	}

	protected static Test baseSuite(String name) {
            BaseTestSuite suite = new BaseTestSuite(name);
        	suite.addTestSuite(ReleaseCompileLocksTest.class);
        	
		return new CleanDatabaseTestSetup(suite) 
        	{
            		protected void decorateSQL(Statement s) throws SQLException
            		{
				s.execute("create table t1 (s int)");	
            		}
        	};
    	} 

        /*Fixtures*/
        public void testReleaseCompileLocks() throws Exception {
            
            Statement stmt = createStatement();

    		// Calling the method dmlstatic with jsr169 will not work because
    		// the procedures use DriverManager to get the default connection.
    		// Of course, this makes this test not fully useful with jsr169,
    		// but at least performing the call to locktable is performed.
            if (JDBC.vmSupportsJDBC3()) 
            	JDBC.assertFullResultSet(stmt.executeQuery(
            		"select (dmlstatic()) from sys.systables where " +
        			"CAST(tablename AS VARCHAR(128))= 'SYSCONGLOMERATES'"), new String[][] {{"1"}});
    		else
    			JDBC.assertFullResultSet(stmt.executeQuery(
            		"select count(*) from sys.systables where " +
            		"CAST(tablename AS VARCHAR(128)) = 'SYSCONGLOMERATES'"), new String[][] {{"1"}});


        assertNoLocks(stmt);
		commit();

		stmt.execute("drop table t1");
		stmt.execute("create table t1 (s int)");
		commit();

		// Calling the method insertstatic with jsr169 will not work because
		// the procedures use DriverManager to get the default connection.
		// Of course, this makes this test not fully useful with jsr169,
		// but at least performing the call to locktable is performed.
		if (JDBC.vmSupportsJDBC3())
			JDBC.assertFullResultSet(stmt.executeQuery(
        		"select (insertstatic()) from sys.systables where " +
        		"CAST(tablename AS VARCHAR(128)) = 'SYSCONGLOMERATES'"), new String[][] {{"1"}});
		else
			JDBC.assertFullResultSet(stmt.executeQuery(
        		"select count(*) from sys.systables where " +
        		"CAST(tablename AS VARCHAR(128)) = 'SYSCONGLOMERATES'"), new String[][] {{"1"}});
			
        assertNoLocks(stmt);

		JDBC.assertEmpty(stmt.executeQuery("select * from t1"));
		stmt.execute("drop table t1");
		commit();

        assertNoLocks(stmt);
		commit();

		stmt.execute("create table test_tab (x int)");
		stmt.executeUpdate("insert into test_tab values (1)");
		commit();

        assertNoLocks(stmt);
		JDBC.assertSingleValueResultSet(stmt.executeQuery("select count(*) from sys.sysviews"), "0");
        assertNoLocks(stmt);
		stmt.execute("insert into test_tab values (2)");

        waitForPostCommit(stmt);
                ResultSet rs = stmt.executeQuery("select TYPE, MODE, TABLENAME, LOCKNAME, STATE from syscs_diag.lock_table order by 1");
		
		String expectedValues[][] = {{"ROW", "X", "TEST_TAB", "(1,8)", "GRANT" }, {"TABLE", "IX", "TEST_TAB", "Tablelock","GRANT"}};
                JDBC.assertFullResultSet(rs, expectedValues);

		try { 
		  	stmt.execute("drop table t1");
		  	fail ("expected SQLException; table t should not exist");
		} catch (SQLException e) {
			assertSQLState("42Y55", e);
		}
		stmt.execute("create table t1 (x int)");
		commit();
		
		JDBC.assertEmpty(stmt.executeQuery("select * from t1"));

		Connection conn1 = openDefaultConnection();
                Statement stmt2 = conn1.createStatement();
		stmt2.execute("create table t2 (x int)");
		stmt2.execute("drop table t2");
		stmt2.close();
        conn1.commit();
		conn1.close();

		stmt.execute("drop table test_tab");
		stmt.execute("create table test_tab (x int)");
		stmt.execute("insert into test_tab values (1)");
		commit();

		PreparedStatement ps = prepareStatement("update test_tab set x=2 where x=?");
		ps.setCursorName("cursor1");
		ps.setInt(1, 1);

        assertNoLocks(stmt);
		ps.executeUpdate();

        waitForPostCommit(stmt);
		rs = stmt.executeQuery("select TYPE, MODE, TABLENAME, LOCKNAME, STATE from syscs_diag.lock_table order by 1");
		String expectedValues1[][] = {{"ROW", "X", "TEST_TAB", "(1,7)", "GRANT" }, {"TABLE", "IX", "TEST_TAB", "Tablelock","GRANT"}};
                JDBC.assertFullResultSet(rs, expectedValues1);
		commit();

		
		stmt.execute("create table t (c1 int not null primary key, c2 int references t)");
		stmt.executeUpdate("insert into t values (1,1)");
		stmt.executeUpdate("insert into t values (2,1)");
		commit();

		ps = prepareStatement("select * from t where c1 = ? and c2 = ?");
		ps.setCursorName("ps");
        assertNoLocks(stmt);

		
		stmt.execute("create table x(c1 int)");
		stmt.execute("drop table x");
		commit();

		ps = prepareStatement("insert into t values (3,2)");
		ps.setCursorName("pi");
        assertNoLocks(stmt);
		commit();


		stmt.execute("create table x(c1 int)");
		stmt.execute("drop table x");
		commit();

		ps = prepareStatement("update t set c2 = c1, c1 = c2");
		ps.setCursorName("p1");
        assertNoLocks(stmt);
		commit();

		
		stmt.execute("create table x(c1 int)");
		stmt.execute("drop table x");
		commit();

		ps = prepareStatement("delete from t");
		ps.setCursorName("p1");
        assertNoLocks(stmt);
		commit();
		
		stmt.execute("create trigger update_of_t after update on t for each row values 2");
		stmt.execute("create trigger insert_of_t after insert on t for each row values 3");
		commit();
	
		ps = prepareStatement("update t set c2=2 where c1=2");
		ps.setCursorName("pu");
        assertNoLocks(stmt);
		commit();

		rs.close();
		ps.close();
		stmt.close();
       }

    /**
     * Assert that the lock table is empty.
     * @param stmt the statement to use for querying the lock table
     */
    private void assertNoLocks(Statement stmt) throws SQLException {
        // First make sure there are no locks held by the post-commit worker
        // thread (DERBY-3258).
        waitForPostCommit(stmt);

        // Then verify that the lock table is empty.
        JDBC.assertEmpty(
                stmt.executeQuery("SELECT * FROM SYSCS_DIAG.LOCK_TABLE"));
    }

    /**
     * Wait for post commit to finish.
     * @param stmt the statement to use for invoking WAIT_FOR_POST_COMMIT
     */
    private void waitForPostCommit(Statement stmt) throws SQLException {
        stmt.execute("CALL WAIT_FOR_POST_COMMIT()");
    }
}