001: // jTDS JDBC Driver for Microsoft SQL Server and Sybase
002: // Copyright (C) 2004 The jTDS Project
003: //
004: // This library is free software; you can redistribute it and/or
005: // modify it under the terms of the GNU Lesser General Public
006: // License as published by the Free Software Foundation; either
007: // version 2.1 of the License, or (at your option) any later version.
008: //
009: // This library is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: // Lesser General Public License for more details.
013: //
014: // You should have received a copy of the GNU Lesser General Public
015: // License along with this library; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: //
018: package net.sourceforge.jtds.test;
019:
020: import java.sql.*;
021:
022: /**
023: * @version 1.0
024: */
025: public class SavepointTest extends TestBase {
026: public SavepointTest(String name) {
027: super (name);
028: }
029:
030: public void testSavepoint1() throws Exception {
031: Statement stmt = con.createStatement();
032: stmt.execute("CREATE TABLE #savepoint1 (data int)");
033: stmt.close();
034:
035: con.setAutoCommit(false);
036:
037: PreparedStatement pstmt = con
038: .prepareStatement("INSERT INTO #savepoint1 (data) VALUES (?)");
039:
040: pstmt.setInt(1, 1);
041: assertTrue(pstmt.executeUpdate() == 1);
042:
043: Savepoint savepoint = con.setSavepoint();
044:
045: assertNotNull(savepoint);
046: assertTrue(savepoint.getSavepointId() == 1);
047:
048: try {
049: savepoint.getSavepointName();
050: assertTrue(false);
051: } catch (SQLException e) {
052: // Ignore, we should get this exception
053: }
054:
055: pstmt.setInt(1, 2);
056: assertTrue(pstmt.executeUpdate() == 1);
057: pstmt.close();
058:
059: stmt = con.createStatement();
060: ResultSet rs = stmt
061: .executeQuery("SELECT SUM(data) FROM #savepoint1");
062:
063: assertTrue(rs.next());
064: assertTrue(rs.getInt(1) == 3);
065: assertTrue(!rs.next());
066: stmt.close();
067: rs.close();
068:
069: con.rollback(savepoint);
070: con.commit();
071:
072: stmt = con.createStatement();
073: rs = stmt.executeQuery("SELECT SUM(data) FROM #savepoint1");
074:
075: assertTrue(rs.next());
076: assertTrue(rs.getInt(1) == 1);
077: assertTrue(!rs.next());
078: stmt.close();
079: rs.close();
080:
081: con.setAutoCommit(true);
082: }
083:
084: public void testSavepoint2() throws Exception {
085: String savepointName = "SAVEPOINT_1";
086:
087: Statement stmt = con.createStatement();
088: stmt.execute("CREATE TABLE #savepoint2 (data int)");
089: stmt.close();
090:
091: con.setAutoCommit(false);
092:
093: PreparedStatement pstmt = con
094: .prepareStatement("INSERT INTO #savepoint2 (data) VALUES (?)");
095:
096: pstmt.setInt(1, 1);
097: assertTrue(pstmt.executeUpdate() == 1);
098:
099: Savepoint savepoint = con.setSavepoint(savepointName);
100:
101: assertNotNull(savepoint);
102: assertTrue(savepointName.equals(savepoint.getSavepointName()));
103:
104: try {
105: savepoint.getSavepointId();
106: assertTrue(false);
107: } catch (SQLException e) {
108: // Ignore, we should get this exception
109: }
110:
111: pstmt.setInt(1, 2);
112: assertTrue(pstmt.executeUpdate() == 1);
113: pstmt.close();
114:
115: stmt = con.createStatement();
116: ResultSet rs = stmt
117: .executeQuery("SELECT SUM(data) FROM #savepoint2");
118:
119: assertTrue(rs.next());
120: assertTrue(rs.getInt(1) == 3);
121: assertTrue(!rs.next());
122: stmt.close();
123: rs.close();
124:
125: con.rollback(savepoint);
126:
127: try {
128: con.rollback(null);
129: assertTrue(false);
130: } catch (SQLException e) {
131: // Ignore, we should get this exception
132: }
133:
134: try {
135: con.rollback(savepoint);
136: assertTrue(false);
137: } catch (SQLException e) {
138: // Ignore, we should get this exception
139: }
140:
141: try {
142: con.releaseSavepoint(null);
143: assertTrue(false);
144: } catch (SQLException e) {
145: // Ignore, we should get this exception
146: }
147:
148: try {
149: con.releaseSavepoint(savepoint);
150: assertTrue(false);
151: } catch (SQLException e) {
152: // Ignore, we should get this exception
153: }
154:
155: con.commit();
156:
157: stmt = con.createStatement();
158: rs = stmt.executeQuery("SELECT SUM(data) FROM #savepoint2");
159:
160: assertTrue(rs.next());
161: assertTrue(rs.getInt(1) == 1);
162: assertTrue(!rs.next());
163: stmt.close();
164: rs.close();
165:
166: con.setAutoCommit(true);
167:
168: try {
169: con.setSavepoint();
170: assertTrue(false);
171: } catch (SQLException e) {
172: // Ignore, we should get this exception
173: }
174:
175: try {
176: con.setSavepoint(savepointName);
177: assertTrue(false);
178: } catch (SQLException e) {
179: // Ignore, we should get this exception
180: }
181: }
182:
183: public void testSavepoint3() throws Exception {
184: Statement stmt = con.createStatement();
185: stmt.execute("CREATE TABLE #savepoint3 (data int)");
186: stmt.close();
187:
188: con.setAutoCommit(false);
189:
190: PreparedStatement pstmt = con
191: .prepareStatement("INSERT INTO #savepoint3 (data) VALUES (?)");
192:
193: pstmt.setInt(1, 1);
194: assertTrue(pstmt.executeUpdate() == 1);
195:
196: Savepoint savepoint1 = con.setSavepoint();
197:
198: assertNotNull(savepoint1);
199: assertTrue(savepoint1.getSavepointId() == 1);
200:
201: pstmt.setInt(1, 2);
202: assertTrue(pstmt.executeUpdate() == 1);
203:
204: Savepoint savepoint2 = con.setSavepoint();
205:
206: assertNotNull(savepoint2);
207: assertTrue(savepoint2.getSavepointId() == 2);
208:
209: pstmt.setInt(1, 3);
210: assertTrue(pstmt.executeUpdate() == 1);
211:
212: Savepoint savepoint3 = con.setSavepoint();
213:
214: assertNotNull(savepoint3);
215: assertTrue(savepoint3.getSavepointId() == 3);
216:
217: pstmt.setInt(1, 4);
218: assertTrue(pstmt.executeUpdate() == 1);
219:
220: pstmt.close();
221:
222: stmt = con.createStatement();
223: ResultSet rs = stmt
224: .executeQuery("SELECT SUM(data) FROM #savepoint3");
225:
226: assertTrue(rs.next());
227: assertTrue(rs.getInt(1) == 10);
228: assertTrue(!rs.next());
229: stmt.close();
230: rs.close();
231:
232: con.releaseSavepoint(savepoint1);
233:
234: try {
235: con.rollback(savepoint1);
236: assertTrue(false);
237: } catch (SQLException e) {
238: // Ignore, we should get this exception
239: }
240:
241: try {
242: con.releaseSavepoint(savepoint1);
243: assertTrue(false);
244: } catch (SQLException e) {
245: // Ignore, we should get this exception
246: }
247:
248: con.rollback(savepoint2);
249:
250: try {
251: con.rollback(savepoint2);
252: assertTrue(false);
253: } catch (SQLException e) {
254: // Ignore, we should get this exception
255: }
256:
257: try {
258: con.releaseSavepoint(savepoint2);
259: assertTrue(false);
260: } catch (SQLException e) {
261: // Ignore, we should get this exception
262: }
263:
264: try {
265: con.rollback(savepoint3);
266: assertTrue(false);
267: } catch (SQLException e) {
268: // Ignore, we should get this exception
269: }
270:
271: try {
272: con.releaseSavepoint(savepoint3);
273: assertTrue(false);
274: } catch (SQLException e) {
275: // Ignore, we should get this exception
276: }
277:
278: con.commit();
279:
280: stmt = con.createStatement();
281: rs = stmt.executeQuery("SELECT SUM(data) FROM #savepoint3");
282:
283: assertTrue(rs.next());
284: assertTrue(rs.getInt(1) == 3);
285: assertTrue(!rs.next());
286: stmt.close();
287: rs.close();
288:
289: con.setAutoCommit(true);
290: }
291:
292: /**
293: * Test to ensure savepoint ids restart at 1. Also ensures that the
294: * procedure cache is managed properly with savepoints.
295: */
296: public void testSavepoint4() throws Exception {
297: Statement stmt = con.createStatement();
298: stmt.execute("CREATE TABLE #savepoint4 (data int)");
299: stmt.close();
300:
301: con.setAutoCommit(false);
302:
303: for (int i = 0; i < 3; i++) {
304: System.out.println("iteration: " + i);
305: PreparedStatement pstmt = con
306: .prepareStatement("INSERT INTO #savepoint4 (data) VALUES (?)");
307:
308: pstmt.setInt(1, 1);
309: assertTrue(pstmt.executeUpdate() == 1);
310:
311: Savepoint savepoint = con.setSavepoint();
312: assertNotNull(savepoint);
313: assertTrue(savepoint.getSavepointId() == 1);
314:
315: try {
316: savepoint.getSavepointName();
317: assertTrue(false);
318: } catch (SQLException e) {
319: // Ignore, we should get this exception
320: }
321:
322: pstmt.setInt(1, 2);
323: assertTrue(pstmt.executeUpdate() == 1);
324: pstmt.close();
325:
326: pstmt = con
327: .prepareStatement("SELECT SUM(data) FROM #savepoint4");
328: ResultSet rs = pstmt.executeQuery();
329:
330: assertTrue(rs.next());
331: assertTrue(rs.getInt(1) == 3);
332: assertTrue(!rs.next());
333: pstmt.close();
334: rs.close();
335:
336: con.rollback(savepoint);
337:
338: pstmt = con
339: .prepareStatement("SELECT SUM(data) FROM #savepoint4");
340: rs = pstmt.executeQuery();
341:
342: assertTrue(rs.next());
343: assertTrue(rs.getInt(1) == 1);
344: assertTrue(!rs.next());
345: pstmt.close();
346: rs.close();
347:
348: con.rollback();
349: }
350:
351: con.setAutoCommit(true);
352: }
353:
354: /**
355: * Test to ensure savepoints can be created even when no statements have
356: * been issued.
357: */
358: public void testSavepoint5() throws Exception {
359: con.setAutoCommit(false);
360: con.setSavepoint();
361: con.rollback();
362: con.setAutoCommit(true);
363: }
364:
365: public static void main(String[] args) {
366: junit.textui.TestRunner.run(SavepointTest.class);
367: }
368: }
|