File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\jdbc\JdbcSavepoint.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.jdbc;
8
9 import java.sql.SQLException;
10 import java.sql.Savepoint;
11
12 import org.h2.constant.ErrorCode;
13 import org.h2.message.DbException;
14 import org.h2.message.Trace;
15 import org.h2.message.TraceObject;
16 import org.h2.util.StringUtils;
17
18 /**
19 * A savepoint is a point inside a transaction to where a transaction can be
20 * rolled back. The tasks that where done before the savepoint are not rolled
21 * back in this case.
22 */
23 public class JdbcSavepoint extends TraceObject implements Savepoint {
24
25 private static final String SYSTEM_SAVEPOINT_PREFIX = "SYSTEM_SAVEPOINT_";
26
27 private final int savepointId;
28 private final String name;
29 private JdbcConnection conn;
30
31 JdbcSavepoint(JdbcConnection conn, int savepointId, String name, Trace trace, int id) {
32 setTrace(trace, TraceObject.SAVEPOINT, id);
33 this.conn = conn;
34 this.savepointId = savepointId;
35 this.name = name;
36 }
37
38 /**
39 * Release this savepoint. This method only set the connection to null and
40 * does not execute a statement.
41 */
42 void release() {
43 this.conn = null;
44 }
45
46 /**
47 * Get the savepoint name for this name or id.
48 * If the name is null, the id is used.
49 *
50 * @param name the name (may be null)
51 * @param id the id
52 * @return the savepoint name
53 */
54 static String getName(String name, int id) {
55 if (name != null) {
56 return StringUtils.quoteJavaString(name);
57 }
58 return SYSTEM_SAVEPOINT_PREFIX + id;
59 }
60
61 /**
62 * Roll back to this savepoint.
63 */
64 void rollback() {
65 checkValid();
66 conn.prepareCommand("ROLLBACK TO SAVEPOINT " + getName(name, savepointId), Integer.MAX_VALUE).executeUpdate();
67 }
68
69 private void checkValid() {
70 if (conn == null) {
71 throw DbException.get(ErrorCode.SAVEPOINT_IS_INVALID_1, getName(name, savepointId));
72 }
73 }
74
75 /**
76 * Get the generated id of this savepoint.
77 * @return the id
78 */
79 public int getSavepointId() throws SQLException {
80 try {
81 debugCodeCall("getSavepointId");
82 checkValid();
83 if (name != null) {
84 throw DbException.get(ErrorCode.SAVEPOINT_IS_NAMED);
85 }
86 return savepointId;
87 } catch (Exception e) {
88 throw logAndConvert(e);
89 }
90 }
91
92 /**
93 * Get the name of this savepoint.
94 * @return the name
95 */
96 public String getSavepointName() throws SQLException {
97 try {
98 debugCodeCall("getSavepointName");
99 checkValid();
100 if (name == null) {
101 throw DbException.get(ErrorCode.SAVEPOINT_IS_UNNAMED);
102 }
103 return name;
104 } catch (Exception e) {
105 throw logAndConvert(e);
106 }
107 }
108
109 /**
110 * INTERNAL
111 */
112 public String toString() {
113 return getTraceObjectName() + ": id=" + savepointId + " name=" + name;
114 }
115
116
117 }
118