1 5 package org.h2.jdbc; 6 7 import java.sql.SQLException ; 8 import java.sql.Savepoint ; 10 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 21 public class JdbcSavepoint extends TraceObject 22 implements Savepoint 24 { 26 27 static final String SYSTEM_SAVEPOINT_PREFIX = "SYSTEM_SAVEPOINT_"; 28 29 private int savepointId; 30 private String name; 31 private JdbcConnection conn; 32 33 JdbcSavepoint(JdbcConnection conn, int savepointId, String 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 getName(String 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 { 53 checkValid(); 54 conn.prepareCommand("ROLLBACK TO SAVEPOINT " + getName(name, savepointId)).executeUpdate(); 55 } 56 57 private void checkValid() throws SQLException { 58 if (conn == null) { 59 throw Message.getSQLException(Message.SAVEPOINT_IS_INVALID_1, getName(name, savepointId)); 60 } 61 } 62 63 67 public int getSavepointId() throws SQLException { 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 e) { 76 throw logAndConvert(e); 77 } 78 } 79 80 84 public String getSavepointName() throws SQLException { 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 e) { 93 throw logAndConvert(e); 94 } 95 } 96 97 } 98 | Popular Tags |