1 18 package org.objectweb.speedo.mapper.rdb; 19 20 import org.objectweb.perseus.persistence.api.ConnectionHolder; 21 import org.objectweb.perseus.persistence.api.PersistenceException; 22 import org.objectweb.perseus.persistence.api.WorkingSet; 23 import org.objectweb.util.monolog.api.BasicLevel; 24 import org.objectweb.util.monolog.api.Logger; 25 import org.objectweb.jorm.api.PMapper; 26 import org.objectweb.jorm.api.PException; 27 import org.objectweb.speedo.pm.api.ProxyManager; 28 import org.objectweb.speedo.workingset.api.Transaction; 29 30 import java.sql.Connection ; 31 import java.sql.SQLException ; 32 33 39 public class JDBCConnectionHolder implements ConnectionHolder { 40 41 44 protected PMapper mapper; 45 46 49 protected WorkingSet workingSet; 50 51 54 protected Connection connection; 55 56 60 protected boolean transactional = false; 61 62 67 protected boolean txManagedExternaly = false; 68 69 protected Logger logger; 70 71 public JDBCConnectionHolder(PMapper mapper, Logger logger) { 72 this.mapper = mapper; 73 this.logger = logger; 74 } 75 76 80 protected void beginLocalTransaction() throws PersistenceException { 81 if (logger !=null && logger.isLoggable(BasicLevel.DEBUG)) { 82 logger.log(BasicLevel.DEBUG, 83 "Begin a local transaction on the connection: " + connection); 84 } 85 try { 86 connection.setAutoCommit(false); 87 } catch (SQLException e) { 88 throw new PersistenceException(e); 89 } 90 } 91 92 95 protected void commitLocalTransaction() throws PersistenceException { 96 if (logger !=null && logger.isLoggable(BasicLevel.DEBUG)) { 97 logger.log(BasicLevel.DEBUG, 98 "Commit the local transaction on the connection: " + connection); 99 } 100 try { 101 connection.commit(); 102 connection.setAutoCommit(true); 103 } catch (SQLException e) { 104 throw new PersistenceException(e); 105 } 106 } 107 108 111 protected void rollbackLocalTransaction() throws PersistenceException { 112 if (logger !=null && logger.isLoggable(BasicLevel.DEBUG)) { 113 logger.log(BasicLevel.DEBUG, 114 "Rollback the local transaction on the connection: " + connection); 115 } 116 try { 117 connection.rollback(); 118 connection.setAutoCommit(true); 119 } catch (SQLException e) { 120 throw new PersistenceException(e); 121 } 122 } 123 124 127 public WorkingSet getWorkingSet() { 128 return workingSet; 129 } 130 131 public void bindWorkingSet(WorkingSet workingSet) { 132 this.workingSet = workingSet; 133 } 134 135 public Object getCHConnectionForRead() throws PersistenceException { 136 return getCHConnectionForWrite(); 137 } 138 139 public Object getCHConnectionForWrite() throws PersistenceException { 140 if (connection == null) { 141 Object cs = null; 142 if (workingSet instanceof Transaction) { 143 Transaction tx = (Transaction) workingSet; 144 cs = ((ProxyManager) tx.getPersistenceManager()) 145 .getConnectionSpec(); 146 txManagedExternaly = tx.isManagedEnv(); 147 } 148 try { 149 if (cs == null) { 150 connection = (Connection ) mapper.getConnection(); 151 } else { 152 connection = (Connection ) mapper.getConnection(cs); 153 } 154 } catch (PException e) { 155 throw new PersistenceException("Impossible to fetch a SQL connection", e); 156 } 157 if (logger !=null && logger.isLoggable(BasicLevel.DEBUG)) { 158 logger.log(BasicLevel.DEBUG, "Allocate a connection: " + connection); 159 } 160 if (transactional && !txManagedExternaly) { 161 beginLocalTransaction(); 162 } 163 } 164 return connection; 165 } 166 167 173 public void begin() throws PersistenceException { 174 transactional = true; 175 if (connection != null) { 176 beginLocalTransaction(); 177 } 178 } 179 180 public void commitCH() throws PersistenceException { 181 try { 182 if (connection != null && !txManagedExternaly && transactional) { 183 commitLocalTransaction(); 184 } 185 } finally { 186 transactional = false; 187 } 188 } 189 190 public void rollbackCH() throws PersistenceException { 191 try { 192 if (connection != null && !txManagedExternaly && transactional) { 193 rollbackLocalTransaction(); 194 } 195 } finally { 196 transactional = false; 197 } 198 } 199 200 public void releaseCHConnection() throws PersistenceException { 201 if (connection != null && txManagedExternaly) { 202 if (logger !=null && logger.isLoggable(BasicLevel.DEBUG)) { 203 logger.log(BasicLevel.DEBUG, "Release the connection " + connection); 204 } 205 closeCHConnection(); 206 } 207 } 208 209 public void closeCHConnection() throws PersistenceException { 210 if (connection != null) { 211 if (logger !=null && logger.isLoggable(BasicLevel.DEBUG)) { 212 logger.log(BasicLevel.DEBUG, "Close the connection: " + connection); 213 } 214 try { 215 mapper.closeConnection(connection); 216 } catch (PException e) { 217 throw new PersistenceException(e); 218 } finally { 219 connection = null; 220 } 221 } 222 } 223 } 224 | Popular Tags |