File: TruncateTableAndOnlineBackupTest.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 (343 lines) | stat: -rw-r--r-- 13,109 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
/*
 * Class org.apache.derbyTesting.functionTests.tests.lang.TruncateTableAndOnlineBackupTest
 *
 * 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.io.File;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
import junit.framework.Test;
import org.apache.derbyTesting.functionTests.util.PrivilegedFileOpsForTests;
import org.apache.derbyTesting.functionTests.util.SQLStateConstants;
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.JDBCDataSource;
import org.apache.derbyTesting.junit.TestConfiguration;

/**
 * Tests interaction of TRUNCATE TABLE and Online Backup.
 * See also DERBY-5213
 * The test needs to test:
 * o uncommitted truncate table followed by online backup; 
 *   then access the backup copy and access the table.
 *   expected behavior: should see the old data.
 * o uncommitted truncate table, followed by online backup that keeps logs,
 *   then commit the truncate, then access the table in the backup. 
 *   expected behavior: should see old data in backup.
 */

public class TruncateTableAndOnlineBackupTest  extends BaseJDBCTestCase {
    
    static String home = null; // derby.system.home
    final static String dbName = "TTOB_db";
//    final static String dbName2 = dbName + "2";
    final static String backupDir = "TTOB_backup";
        
    public TruncateTableAndOnlineBackupTest(String name) {
        super(name);
    }

    public static Test suite() {
        BaseTestSuite suite =
            new BaseTestSuite("TruncateTableAndOnlineBackupTest");

        suite.addTest(baseSuite("TruncateTableAndOnlineBackupTest:Embedded"));
        //suite.addTest(TestConfiguration
        //        .clientServerDecorator(baseSuite("TruncateTableAndOnlineBackupTest:Client")));
        return TestConfiguration.singleUseDatabaseDecorator(suite,
                dbName);

        //return suite;
    }
    
    protected static Test baseSuite(String name) {
        BaseTestSuite suite = new BaseTestSuite(name);
        suite.addTestSuite(TruncateTableAndOnlineBackupTest.class);
        return new CleanDatabaseTestSetup(suite); 
    }
    
    public void setUp() throws Exception {
        getConnection();
        home = getSystemProperty("derby.system.home");

        Statement stmt = createStatement();
        stmt.executeUpdate("create table truncable(i int)");
        PreparedStatement ps = getConnection().prepareStatement(
                "insert into truncable values (?)");

        // insert some data
        getConnection().setAutoCommit(false);
        for (int i=1; i <= 1000; i++) {
            ps.setInt(1,i);
            ps.executeUpdate();
        }
        getConnection().commit();
    }
    
    /*
     * Drop the table truncable that was created in setUp().
     */
    public void tearDown() throws Exception
    {
        getConnection().createStatement().execute("drop table truncable");
        super.tearDown();
    }

    /*  uncommitted truncate table followed by online backup; 
     *  then access the backup copy and access the table.
     *  expected behavior: should see the old data.
     */
    public void testUncommittedTruncateBasicBackup() throws Exception {

        setAutoCommit(false);
        Statement s = createStatement();

        // check...we should have 1000 rows
        JDBC.assertFullResultSet(
                s.executeQuery("select count(*) from truncable"),
                new String[][]{{"1000"}});

        // truncate the table, but do not commit
        s.executeUpdate("truncate table truncable");

        // check...we should have no rows
        ResultSet rs = s.executeQuery("select * from truncable");
        JDBC.assertEmpty(rs);
        
        CallableStatement cs = prepareCall
            ("CALL SYSCS_UTIL.SYSCS_BACKUP_DATABASE(?)");
        String fullBackupDir = home + "/" + backupDir;
        cs.setString(1, fullBackupDir);
        cs.execute();

        // check contents of table in backup dir

        final DataSource ds2 = JDBCDataSource.getDataSource(fullBackupDir+"/"+dbName);
        final Connection con2 = ds2.getConnection();
        Statement s2 = con2.createStatement();
        // check...we should have 1000 rows because truncate table was not committed
        JDBC.assertFullResultSet(
                s2.executeQuery("select count(*) from truncable"),
                new String[][]{{"1000"}});

        con2.close();

        // close down both
        final DataSource[] srcs =
                new DataSource[] {JDBCDataSource.getDataSource(),
                    JDBCDataSource.getDataSource(fullBackupDir+"/"+dbName)};

        for (int i=0; i < srcs.length; i++) {
            JDBCDataSource.setBeanProperty(
                    srcs[i], "connectionAttributes", "shutdown=true");

            try {
                srcs[i].getConnection();
                fail("shutdown failed: expected exception");
            } catch (SQLException e) {
                assertSQLState(
                    "database shutdown",
                    SQLStateConstants.CONNECTION_EXCEPTION_CONNECTION_FAILURE,
                    e);
            }
        }

        assertDirectoryDeleted(new File(fullBackupDir));
    }

    /*   uncommitted truncate table, followed by online backup that keeps logs,
     *   then commit the truncate, then access the table in the backup. 
     *   expected behavior: should see old data in backup.
     */
    public void testUncommittedTruncateBackupEnableLog() throws Exception {

        setAutoCommit(false);
        Statement s = createStatement();

        // check...we should have 1000 rows
        JDBC.assertFullResultSet(
                s.executeQuery("select count(*) from truncable"),
                new String[][]{{"1000"}});

        // truncate the table, but do not commit
        s.executeUpdate("truncate table truncable");

        // check...we should have no rows
        ResultSet rs = s.executeQuery("select * from truncable");
        JDBC.assertEmpty(rs);
        
        CallableStatement cs = prepareCall
            ("CALL SYSCS_UTIL.SYSCS_BACKUP_DATABASE_AND_ENABLE_LOG_ARCHIVE_MODE(?,1)");
        String fullBackupDir = home + "/" + backupDir;
        cs.setString(1, fullBackupDir);
        cs.execute();

        // now commit - this will commit the truncate table
        commit();
        
        // check contents of table in backup dir
        final DataSource ds2 = JDBCDataSource.getDataSource(fullBackupDir+"/"+dbName);
        final Connection con2 = ds2.getConnection();
        Statement s2 = con2.createStatement();
        // we should have 1000 rows because truncate table was not committed
        JDBC.assertFullResultSet(
                s2.executeQuery("select count(*) from truncable"),
                new String[][]{{"1000"}});
        con2.close();

        // backup again, to a different dir
        String fullBackupDir2=fullBackupDir+"2";
        cs.setString(1, fullBackupDir2);
        cs.execute();

        // connect to the second backed up database, but this time,
        // we should not have any rows.
        final DataSource ds3 = JDBCDataSource.getDataSource(fullBackupDir2+"/"+dbName);
        final Connection con3 = ds3.getConnection();
        Statement s3 = con3.createStatement();
        rs = s3.executeQuery("select * from truncable");
        JDBC.assertEmpty(rs);

        // close down all
        final DataSource[] srcs =
                new DataSource[] {JDBCDataSource.getDataSource(),
                    JDBCDataSource.getDataSource(fullBackupDir+"/"+dbName),
                    JDBCDataSource.getDataSource(fullBackupDir2+"/"+dbName)};

        for (int i=0; i < srcs.length; i++) {
            JDBCDataSource.setBeanProperty(
                    srcs[i], "connectionAttributes", "shutdown=true");

            try {
                srcs[i].getConnection();
                fail("shutdown failed: expected exception");
            } catch (SQLException e) {
                assertSQLState(
                    "database shutdown",
                    SQLStateConstants.CONNECTION_EXCEPTION_CONNECTION_FAILURE,
                    e);
            }
        }

        assertDirectoryDeleted(new File(fullBackupDir));
        assertDirectoryDeleted(new File(fullBackupDir2));
    }    
    
    /*  uncommitted truncate table followed by online backup; 
     *  then access the backup copy and access the table.
     *  expected behavior: should see the old data.
     */
    public void testTruncateFreezeUnfreeze() throws Exception {

        setAutoCommit(false);
        Statement s = createStatement();

        // check...we should have 1000 rows
        JDBC.assertFullResultSet(
                s.executeQuery("select count(*) from truncable"),
                new String[][]{{"1000"}});

        // truncate the table, but do not commit
        s.executeUpdate("truncate table truncable");

        // check...we should have no rows
        ResultSet rs = s.executeQuery("select * from truncable");
        JDBC.assertEmpty(rs);

        // freeze the database
        s.execute("call syscs_util.syscs_freeze_database()");

        // Now copy the database directory
        String fullBackupDir = backupDir + "2";
        File DbDir = new File(home, dbName);
        File fullBackupDbDir = new File(home, fullBackupDir );
        PrivilegedFileOpsForTests.copy(DbDir, fullBackupDbDir);
        
        // At this point, writing to the original database is blocked.
        // Try to read from the original database. Should work, we should still
        // be connected, and read access is allowed during the freeze.
        rs = s.executeQuery("select * from truncable");
        JDBC.assertEmpty(rs);

        // connect to backed-up database
        final DataSource ds2 = JDBCDataSource.getDataSource(fullBackupDir);
        final Connection con2 = ds2.getConnection();
        Statement s2 = con2.createStatement();
        // check...we should have 1000 rows because truncate table was not committed
        // before the freeze and copy
        JDBC.assertFullResultSet(
                s2.executeQuery("select count(*) from truncable"),
                new String[][]{{"1000"}});

        // unfreeze our original database
        s.execute("call syscs_util.syscs_unfreeze_database()");

        // ensure we can read and write now.
        rs = s.executeQuery("select * from truncable");
        JDBC.assertEmpty(rs);
        s.executeUpdate("insert into truncable values(2001)");
        JDBC.assertFullResultSet(
                s.executeQuery("select count(*) from truncable"),
                new String[][]{{"1"}});

        // rollback, should rollback the truncate - then 
        // select again from the org db - should have 1000 rows again instead of 1
        // then select again from the backup db, should still have 1000 rows.
        rollback();
        JDBC.assertFullResultSet(
                s.executeQuery("select count(*) from truncable"),
                new String[][]{{"1000"}});
        JDBC.assertFullResultSet(
                s2.executeQuery("select count(*) from truncable"),
                new String[][]{{"1000"}});

        s2.close();
        con2.close();

        // close down both databases
        final DataSource[] srcs =
                new DataSource[] {JDBCDataSource.getDataSource(),
                ds2};

        for (int i=0; i < srcs.length; i++) {
            JDBCDataSource.setBeanProperty(
                    srcs[i], "connectionAttributes", "shutdown=true");
            try {
                srcs[i].getConnection();
                fail("shutdown failed: expected exception");
            } catch (SQLException e) {
                assertSQLState(
                    "database shutdown",
                    SQLStateConstants.CONNECTION_EXCEPTION_CONNECTION_FAILURE,
                    e);
            }
        }

        assertDirectoryDeleted(new File(home + "/"+fullBackupDir));
    }    
}