KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > jdbc > JdbcSavepoint


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.jdbc;
6
7 import java.sql.SQLException JavaDoc;
8 //#ifdef JDK14
9
import java.sql.Savepoint JavaDoc;
10 //#endif
11

12 import org.h2.message.Message;
13 import org.h2.message.Trace;
14 import org.h2.message.TraceObject;
15 import org.h2.util.StringUtils;
16
17 /**
18  * A savepoint is a point inside a transaction to where a transaction can be rolled back.
19  * The tasks that where done before the savepoint are not rolled back in this case.
20  */

21 public class JdbcSavepoint extends TraceObject
22 //#ifdef JDK14
23
implements Savepoint JavaDoc
24 //#endif
25
{
26
27     static final String JavaDoc SYSTEM_SAVEPOINT_PREFIX = "SYSTEM_SAVEPOINT_";
28
29     private int savepointId;
30     private String JavaDoc name;
31     private JdbcConnection conn;
32
33     JdbcSavepoint(JdbcConnection conn, int savepointId, String JavaDoc name, Trace trace, int id) {
34         setTrace(trace, TraceObject.SAVEPOINT, id);
35         this.conn = conn;
36         this.savepointId = savepointId;
37         this.name = name;
38     }
39
40     void release() {
41         this.conn = null;
42     }
43
44     static String JavaDoc getName(String JavaDoc name, int id) {
45         if (name != null) {
46             return StringUtils.quoteJavaString(name);
47         } else {
48             return SYSTEM_SAVEPOINT_PREFIX + id;
49         }
50     }
51
52     void rollback() throws SQLException JavaDoc {
53         checkValid();
54         conn.prepareCommand("ROLLBACK TO SAVEPOINT " + getName(name, savepointId)).executeUpdate();
55     }
56
57     private void checkValid() throws SQLException JavaDoc {
58         if (conn == null) {
59             throw Message.getSQLException(Message.SAVEPOINT_IS_INVALID_1, getName(name, savepointId));
60         }
61     }
62
63     /**
64      * Get the generated id of this savepoint.
65      * @return the id
66      */

67     public int getSavepointId() throws SQLException JavaDoc {
68         try {
69             debugCodeCall("getSavepointId");
70             checkValid();
71             if (name != null) {
72                 throw Message.getSQLException(Message.SAVEPOINT_IS_NAMED);
73             }
74             return savepointId;
75         } catch(Throwable JavaDoc e) {
76             throw logAndConvert(e);
77         }
78     }
79     
80     /**
81      * Get the name of this savepoint.
82      * @return the name
83      */

84     public String JavaDoc getSavepointName() throws SQLException JavaDoc {
85         try {
86             debugCodeCall("getSavepointName");
87             checkValid();
88             if (name == null) {
89                 throw Message.getSQLException(Message.SAVEPOINT_IS_UNNAMED);
90             }
91             return name;
92         } catch(Throwable JavaDoc e) {
93             throw logAndConvert(e);
94         }
95     }
96
97 }
98
Popular Tags