1 16 17 package org.springframework.jdbc.datasource; 18 19 import java.sql.Connection ; 20 import java.sql.SQLException ; 21 22 import org.springframework.transaction.support.ResourceHolderSupport; 23 import org.springframework.util.Assert; 24 25 40 public class ConnectionHolder extends ResourceHolderSupport { 41 42 public static final String SAVEPOINT_NAME_PREFIX = "SAVEPOINT_"; 43 44 45 private ConnectionHandle connectionHandle; 46 47 private Connection currentConnection; 48 49 private boolean transactionActive = false; 50 51 private Boolean savepointsSupported; 52 53 private int savepointCounter = 0; 54 55 56 60 public ConnectionHolder(ConnectionHandle connectionHandle) { 61 Assert.notNull(connectionHandle, "ConnectionHandle must not be null"); 62 this.connectionHandle = connectionHandle; 63 } 64 65 73 public ConnectionHolder(Connection connection) { 74 this.connectionHandle = new SimpleConnectionHandle(connection); 75 } 76 77 85 public ConnectionHolder(Connection connection, boolean transactionActive) { 86 this(connection); 87 this.transactionActive = transactionActive; 88 } 89 90 91 94 public ConnectionHandle getConnectionHandle() { 95 return this.connectionHandle; 96 } 97 98 101 protected boolean hasConnection() { 102 return (this.connectionHandle != null); 103 } 104 105 109 protected void setTransactionActive(boolean transactionActive) { 110 this.transactionActive = transactionActive; 111 } 112 113 116 protected boolean isTransactionActive() { 117 return this.transactionActive; 118 } 119 120 121 127 protected void setConnection(Connection connection) { 128 if (this.currentConnection != null) { 129 this.connectionHandle.releaseConnection(this.currentConnection); 130 this.currentConnection = null; 131 } 132 if (connection != null) { 133 this.connectionHandle = new SimpleConnectionHandle(connection); 134 } 135 else { 136 this.connectionHandle = null; 137 } 138 } 139 140 148 public Connection getConnection() { 149 Assert.notNull(this.connectionHandle, "Active Connection is required"); 150 if (this.currentConnection == null) { 151 this.currentConnection = this.connectionHandle.getConnection(); 152 } 153 return this.currentConnection; 154 } 155 156 161 public boolean supportsSavepoints() throws SQLException { 162 if (this.savepointsSupported == null) { 163 this.savepointsSupported = new Boolean (getConnection().getMetaData().supportsSavepoints()); 164 } 165 return this.savepointsSupported.booleanValue(); 166 } 167 168 174 public Object createSavepoint() throws SQLException { 175 this.savepointCounter++; 176 return getConnection().setSavepoint(SAVEPOINT_NAME_PREFIX + this.savepointCounter); 177 } 178 179 188 public void released() { 189 super.released(); 190 if (this.currentConnection != null) { 191 this.connectionHandle.releaseConnection(this.currentConnection); 192 this.currentConnection = null; 193 } 194 } 195 196 197 public void clear() { 198 super.clear(); 199 this.transactionActive = false; 200 this.savepointsSupported = null; 201 this.savepointCounter = 0; 202 } 203 204 } 205 | Popular Tags |