Repository navigation
Expand file tree
/
Copy pathTransactionTest.java
More file actions
214 lines (195 loc) · 7.42 KB
/
Copy pathTransactionTest.java
File metadata and controls
214 lines (195 loc) · 7.42 KB
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
// 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
// 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 com.cloud.utils.db;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import com.cloud.utils.component.ComponentContext;
import com.cloud.utils.exception.CloudRuntimeException;
/**
* A test fixture to test APIs or bugs found for Transaction class. This test fixture will do one time setup before
* all its testcases to set up a test db table, and then tear down these test db artifacts after all testcases are run.
*
*/
public class TransactionTest {
@BeforeClass
public static void oneTimeSetup() {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = Transaction.getStandaloneConnection();
pstmt = conn.prepareStatement("CREATE TABLE `cloud`.`test` ("
+ "`id` bigint unsigned NOT NULL UNIQUE AUTO_INCREMENT," + "`fld_int` int unsigned,"
+ "`fld_long` bigint unsigned," + "`fld_string` varchar(255)," + "PRIMARY KEY (`id`)"
+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8;");
pstmt.execute();
} catch (SQLException e) {
throw new CloudRuntimeException("Problem with sql", e);
} finally {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
}
}
}
}
@Test
/**
* When a transaction is set to use user-managed db connection, for each following db statement, we should see
* that the same db connection is reused rather than acquiring a new one each time in typical transaction model.
*/
public void testUserManagedConnection() {
DbTestDao testDao = ComponentContext.inject(DbTestDao.class);
Transaction txn = Transaction.open("SingleConnectionThread");
Connection conn = null;
try {
conn = Transaction.getStandaloneConnectionWithException();
txn.transitToUserManagedConnection(conn);
// try two SQLs to make sure that they are using the same connection
// acquired above.
testDao.create(1, 1, "Record 1");
Connection checkConn = Transaction.currentTxn().getConnection();
if (checkConn != conn) {
Assert.fail("A new db connection is acquired instead of using old one after create sql");
}
testDao.update(2, 2, "Record 1");
Connection checkConn2 = Transaction.currentTxn().getConnection();
if (checkConn2 != conn) {
Assert.fail("A new db connection is acquired instead of using old one after update sql");
}
} catch (SQLException e) {
Assert.fail(e.getMessage());
} finally {
txn.transitToAutoManagedConnection(Transaction.CLOUD_DB);
txn.close();
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
throw new CloudRuntimeException("Problem with close db connection", e);
}
}
}
}
@Test
/**
* This test is simulating ClusterHeartBeat process, where the same transaction and db connection is reused.
*/
public void testTransactionReuse() {
DbTestDao testDao = ComponentContext.inject(DbTestDao.class);
// acquire a db connection and keep it
Connection conn = null;
try {
conn = Transaction.getStandaloneConnectionWithException();
} catch (SQLException ex) {
throw new CloudRuntimeException("Problem with getting db connection", ex);
}
// start heartbeat loop, make sure that each loop still use the same
// connection
Transaction txn = null;
for (int i = 0; i < 3; i++) {
txn = Transaction.open("HeartbeatSimulator");
try {
txn.transitToUserManagedConnection(conn);
testDao.create(i, i, "Record " + i);
Connection checkConn = Transaction.currentTxn().getConnection();
if (checkConn != conn) {
Assert.fail("A new db connection is acquired instead of using old one in loop " + i);
}
} catch (SQLException e) {
Assert.fail(e.getMessage());
} finally {
txn.transitToAutoManagedConnection(Transaction.CLOUD_DB);
txn.close();
}
}
// close the connection once we are done since we are managing db
// connection ourselves.
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
throw new CloudRuntimeException("Problem with close db connection", e);
}
}
}
@After
/**
* Delete all records after each test, but table is still kept
*/
public void tearDown() {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = Transaction.getStandaloneConnection();
pstmt = conn.prepareStatement("truncate table `cloud`.`test`");
pstmt.execute();
} catch (SQLException e) {
throw new CloudRuntimeException("Problem with sql", e);
} finally {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
}
}
}
}
@AfterClass
public static void oneTimeTearDown() {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = Transaction.getStandaloneConnection();
pstmt = conn.prepareStatement("DROP TABLE IF EXISTS `cloud`.`test`");
pstmt.execute();
} catch (SQLException e) {
throw new CloudRuntimeException("Problem with sql", e);
} finally {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
}
}
}
}
}