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
|
/*
Derby - Class org.apache.derbyTesting.functionTests.tests.lang.ShutdownDatabaseTest
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;
/**
* This test confirm that no trouble happens when database , of which active
* connection exists with , was shut down.
*/
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
import junit.framework.Test;
import org.apache.derby.jdbc.ClientDataSource;
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;
public class ShutdownDatabaseTest extends BaseJDBCTestCase {
/**
* Public constructor required for running test as standalone JUnit.
*/
public ShutdownDatabaseTest(String name) {
super(name);
}
/**
* Create a suite of tests.
*/
public static Test suite() {
// Only run in embedded as running in client/server
// hits a problem. See DERBY-2477. To see the bug
// just use the defaultSuite.
return new CleanDatabaseTestSetup(new BaseTestSuite(
ShutdownDatabaseTest.class, "ShutdownDatabaseTest"));
}
protected void initializeConnection(Connection conn) throws SQLException {
conn.setAutoCommit(false);
}
protected void setUp() throws SQLException {
Statement stmt = createStatement();
stmt.executeUpdate("create table " + "TEST_TABLE "
+ "( TEST_COL integer )");
commit();
insertIntoTestTable(1, 1000);
}
/**
* to make sure all tables and databases are dropped
*/
protected void tearDown() throws Exception {
Statement st = createStatement();
st.execute("DROP TABLE TEST_TABLE");
st.close();
commit();
super.tearDown();
}
/**
* Tests shutdown with the only transaction was committed.
*/
public void testOnlyTransactionWasCommited() throws SQLException {
commit();
assertShutdownOK();
Statement st = createStatement();
JDBC.assertSingleValueResultSet(st.executeQuery("select " + "count(*) "
+ "from " + "TEST_TABLE "), "1000");
st.close();
}
/**
* Tests shutdown with the transaction was committed, and next transaction was committed.
*/
public void testTwiceCommited() throws SQLException {
commit();
insertIntoTestTable(1001, 999);
commit();
assertShutdownOK();
Statement st = createStatement();
JDBC.assertSingleValueResultSet(st.executeQuery("select " + "count(*) "
+ "from " + "TEST_TABLE "), "1999");
st.close();
}
/**
* Tests shutdown with the transaction was rollbacked, and next transaction was commited.
*/
public void testOnceRollbackedAndCommited() throws SQLException {
rollback();
insertIntoTestTable(1001, 999);
commit();
assertShutdownOK();
Statement st = createStatement();
JDBC.assertSingleValueResultSet(st.executeQuery("select " + "count(*) "
+ "from " + "TEST_TABLE "), "999");
st.close();
}
/**
* Tests shutdown with the only transaction was rollbacked.
*/
public void testOnlyTransactionWasRollbacked() throws SQLException {
rollback();
assertShutdownOK();
Statement st = createStatement();
JDBC.assertSingleValueResultSet(st.executeQuery("select " + "count(*) "
+ "from " + "TEST_TABLE "), "0");
st.close();
}
/**
* Tests shutdown with the transaction was commited, and next transaction was rollbacked.
*/
public void testOnceCommitedAndRollbacked() throws SQLException {
commit();
insertIntoTestTable(1001, 999);
rollback();
assertShutdownOK();
Statement st = createStatement();
JDBC.assertSingleValueResultSet(st.executeQuery("select " + "count(*) "
+ "from " + "TEST_TABLE "), "1000");
st.close();
}
/**
* Tests shutdown with the transaction was rollbacked, and next transaction was rollbacked.
*/
public void testTwiceRollbacked() throws SQLException {
rollback();
insertIntoTestTable(1001, 999);
rollback();
assertShutdownOK();
Statement st = createStatement();
JDBC.assertSingleValueResultSet(st.executeQuery("select " + "count(*) "
+ "from " + "TEST_TABLE "), "0");
st.close();
}
/**
* Tests shutdown with the only transaction was not committed/rollbacked.
*/
public void testOnlyTransactionWasLeft() throws SQLException {
assertShutdownOK();
Statement st = createStatement();
JDBC.assertSingleValueResultSet(st.executeQuery("select " + "count(*) "
+ "from " + "TEST_TABLE "), "0");
st.close();
}
/**
* Tests shutdown with the transaction was committed, and next transaction was not
* committed/rollbacked yet.
*/
public void testOnceCommitedAndLeft() throws SQLException {
commit();
insertIntoTestTable(1001, 999);
assertShutdownOK();
Statement st = createStatement();
JDBC.assertSingleValueResultSet(st.executeQuery("select " + "count(*) "
+ "from " + "TEST_TABLE "), "1000");
st.close();
}
/**
* Tests shutdown with the transaction was rollbacked, and next transaction was not
* committed/rollbacked yet.
*/
public void testOnceRollbackedAndLeft() throws SQLException {
rollback();
insertIntoTestTable(1001, 999);
assertShutdownOK();
Statement st = createStatement();
JDBC.assertSingleValueResultSet(st.executeQuery("select " + "count(*) "
+ "from " + "TEST_TABLE "), "0");
st.close();
}
protected void insertIntoTestTable(int val) throws SQLException {
PreparedStatement st = null;
try {
st = prepareStatement("insert into " + "TEST_TABLE "
+ "( TEST_COL ) " + "values( ? )");
st.setInt(1, val);
st.execute();
} finally {
if (st != null) {
st.close();
st = null;
}
}
}
private void insertIntoTestTable(int initialval, int count)
throws SQLException {
for (int i = initialval; i < initialval + count; i++) {
insertIntoTestTable(i);
}
}
protected void assertShutdownOK() throws SQLException {
Connection conn = getConnection();
if (usingEmbedded()) {
DataSource ds = JDBCDataSource.getDataSource();
JDBCDataSource.setBeanProperty(ds, "shutdownDatabase", "shutdown");
try {
ds.getConnection();
fail("expected shutdown to fail");
} catch (SQLException e) {
// expect 08006 on successful shutdown
assertSQLState("08006", e);
}
assertTrue(conn.isClosed());
} else if (usingDerbyNetClient()) {
ClientDataSource ds = (ClientDataSource) JDBCDataSource
.getDataSource();
ds.setConnectionAttributes("shutdown=true");
try {
ds.getConnection();
fail("expected shutdown to fail");
} catch (SQLException e) {
// expect 08006 on successful shutdown
assertSQLState("08006", e);
}
}
}
}
|