1 16 17 package org.springframework.jdbc.datasource; 18 19 import java.sql.Connection ; 20 import java.sql.SQLException ; 21 22 import org.springframework.core.Constants; 23 import org.springframework.transaction.TransactionDefinition; 24 import org.springframework.transaction.support.DefaultTransactionDefinition; 25 import org.springframework.transaction.support.TransactionSynchronizationManager; 26 27 55 public class IsolationLevelDataSourceAdapter extends UserCredentialsDataSourceAdapter { 56 57 58 private static final Constants constants = new Constants(TransactionDefinition.class); 59 60 private Integer isolationLevel; 61 62 63 77 public final void setIsolationLevelName(String constantName) throws IllegalArgumentException { 78 if (constantName == null || !constantName.startsWith(DefaultTransactionDefinition.PREFIX_ISOLATION)) { 79 throw new IllegalArgumentException ("Only isolation constants allowed"); 80 } 81 setIsolationLevel(constants.asNumber(constantName).intValue()); 82 } 83 84 103 public void setIsolationLevel(int isolationLevel) { 104 if (!constants.getValues(DefaultTransactionDefinition.PREFIX_ISOLATION).contains(new Integer (isolationLevel))) { 105 throw new IllegalArgumentException ("Only values of isolation constants allowed"); 106 } 107 this.isolationLevel = 108 (isolationLevel != TransactionDefinition.ISOLATION_DEFAULT ? new Integer (isolationLevel) : null); 109 } 110 111 115 protected Integer getIsolationLevel() { 116 return this.isolationLevel; 117 } 118 119 120 126 protected Connection doGetConnection(String username, String password) throws SQLException { 127 Connection con = super.doGetConnection(username, password); 128 Boolean readOnlyToUse = getCurrentReadOnlyFlag(); 129 if (readOnlyToUse != null) { 130 con.setReadOnly(readOnlyToUse.booleanValue()); 131 } 132 Integer isolationLevelToUse = getCurrentIsolationLevel(); 133 if (isolationLevelToUse != null) { 134 con.setTransactionIsolation(isolationLevelToUse.intValue()); 135 } 136 return con; 137 } 138 139 146 protected Integer getCurrentIsolationLevel() { 147 Integer isolationLevelToUse = TransactionSynchronizationManager.getCurrentTransactionIsolationLevel(); 148 if (isolationLevelToUse == null) { 149 isolationLevelToUse = getIsolationLevel(); 150 } 151 return isolationLevelToUse; 152 } 153 154 160 protected Boolean getCurrentReadOnlyFlag() { 161 boolean txReadOnly = TransactionSynchronizationManager.isCurrentTransactionReadOnly(); 162 return (txReadOnly ? Boolean.TRUE : null); 163 } 164 165 } 166 | Popular Tags |