1 16 17 package org.springframework.jdbc.datasource; 18 19 import java.sql.Savepoint ; 20 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 24 import org.springframework.transaction.CannotCreateTransactionException; 25 import org.springframework.transaction.NestedTransactionNotSupportedException; 26 import org.springframework.transaction.SavepointManager; 27 import org.springframework.transaction.TransactionException; 28 import org.springframework.transaction.TransactionSystemException; 29 import org.springframework.transaction.TransactionUsageException; 30 import org.springframework.transaction.support.SmartTransactionObject; 31 import org.springframework.util.ClassUtils; 32 33 49 public abstract class JdbcTransactionObjectSupport implements SavepointManager, SmartTransactionObject { 50 51 private static final Log logger = LogFactory.getLog(JdbcTransactionObjectSupport.class); 52 53 private static boolean savepointClassAvailable = 56 ClassUtils.isPresent("java.sql.Savepoint", JdbcTransactionObjectSupport.class.getClassLoader()); 57 58 59 private ConnectionHolder connectionHolder; 60 61 private Integer previousIsolationLevel; 62 63 private boolean savepointAllowed; 64 65 66 public void setConnectionHolder(ConnectionHolder connectionHolder) { 67 this.connectionHolder = connectionHolder; 68 } 69 70 public ConnectionHolder getConnectionHolder() { 71 return connectionHolder; 72 } 73 74 public boolean hasConnectionHolder() { 75 return (this.connectionHolder != null); 76 } 77 78 public void setPreviousIsolationLevel(Integer previousIsolationLevel) { 79 this.previousIsolationLevel = previousIsolationLevel; 80 } 81 82 public Integer getPreviousIsolationLevel() { 83 return previousIsolationLevel; 84 } 85 86 public void setSavepointAllowed(boolean savepointAllowed) { 87 this.savepointAllowed = savepointAllowed; 88 } 89 90 public boolean isSavepointAllowed() { 91 return savepointAllowed; 92 } 93 94 95 99 103 public Object createSavepoint() throws TransactionException { 104 if (!savepointClassAvailable) { 105 throw new NestedTransactionNotSupportedException( 106 "Cannot create a nested JDBC transaction because you are not running on JDK 1.4 or higher"); 107 } 108 ConnectionHolder conHolder = getConnectionHolderForSavepoint(); 109 try { 110 if (!conHolder.supportsSavepoints()) { 111 throw new NestedTransactionNotSupportedException( 112 "Cannot create a nested transaction because savepoints are not supported by your JDBC driver"); 113 } 114 } 115 catch (Throwable ex) { 116 throw new NestedTransactionNotSupportedException( 117 "Cannot create a nested transaction because your JDBC driver is not a JDBC 3.0 driver", ex); 118 } 119 try { 120 return conHolder.createSavepoint(); 121 } 122 catch (Throwable ex) { 123 throw new CannotCreateTransactionException("Could not create JDBC savepoint", ex); 124 } 125 } 126 127 131 public void rollbackToSavepoint(Object savepoint) throws TransactionException { 132 try { 133 getConnectionHolderForSavepoint().getConnection().rollback((Savepoint ) savepoint); 134 } 135 catch (Throwable ex) { 136 throw new TransactionSystemException("Could not roll back to JDBC savepoint", ex); 137 } 138 } 139 140 144 public void releaseSavepoint(Object savepoint) throws TransactionException { 145 try { 146 getConnectionHolderForSavepoint().getConnection().releaseSavepoint((Savepoint ) savepoint); 147 } 148 catch (Throwable ex) { 149 logger.debug("Could not explicitly release JDBC savepoint", ex); 150 } 151 } 152 153 protected ConnectionHolder getConnectionHolderForSavepoint() throws TransactionException { 154 if (!isSavepointAllowed()) { 155 throw new NestedTransactionNotSupportedException( 156 "Transaction manager does not allow nested transactions"); 157 } 158 if (!hasConnectionHolder()) { 159 throw new TransactionUsageException( 160 "Cannot create nested transaction if not exposing a JDBC transaction"); 161 } 162 return getConnectionHolder(); 163 } 164 165 } 166 | Popular Tags |