1 package org.apache.ojb.jdori.sql; 2 16 17 import javax.jdo.JDODataStoreException; 18 import javax.jdo.JDOUserException; 19 20 import org.apache.ojb.broker.PersistenceBroker; 21 import org.apache.ojb.broker.PersistenceBrokerFactory; 22 import org.apache.ojb.broker.util.logging.Logger; 23 import org.apache.ojb.broker.util.logging.LoggerFactory; 24 25 import com.sun.jdori.Connector; 26 27 32 class OjbStoreConnector implements Connector 33 { 34 37 private boolean rollbackOnlyFlag = false; 38 39 42 private final OjbStorePMF pmf; 43 44 47 PersistenceBroker broker = null; 48 49 52 private boolean connectionReadyForRelease = true; 53 54 57 private Logger logger = LoggerFactory.getLogger("JDO"); 58 59 OjbStoreConnector(OjbStorePMF pmf) 60 { 61 this.pmf = pmf; 62 } 63 64 65 68 public void begin(boolean optimistic) 69 { 70 assertNotRollbackOnly(); 71 72 connectionReadyForRelease = false; 73 logger.debug("OjbStoreConnector.begin: connectionReadyForRelease=" + connectionReadyForRelease); 74 75 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 77 broker.beginTransaction(); 78 } 79 80 83 public void beforeCompletion() 84 { 85 assertNotRollbackOnly(); 86 } 88 89 92 public void flush() 93 { 94 assertNotRollbackOnly(); 95 logger.debug("OjbStoreConnector.flush: " + "connectionReadyForRelease=" + connectionReadyForRelease); 96 } 98 99 102 public synchronized void commit() 103 { 104 assertNotRollbackOnly(); 105 106 try 107 { 108 logger.debug("OjbStoreConnector.commit"); 109 broker.commitTransaction(); 110 broker.close(); 111 broker = null; 112 } 113 catch (Exception ex) 114 { 115 throw new OjbStoreFatalInternalException(getClass(), "commit", ex); 116 } 117 finally 118 { 119 connectionReadyForRelease = true; 120 } 121 } 122 123 126 public synchronized void rollback() 127 { 128 logger.debug("OjbStoreConnector.rollback"); 129 130 if (!rollbackOnlyFlag) 131 { 132 try 133 { 134 broker.abortTransaction(); 135 broker.close(); 136 broker = null; 137 } 138 catch (Exception ex) 139 { 140 throw new OjbStoreFatalInternalException(getClass(), "rollback", ex); 141 } 142 finally 143 { 144 connectionReadyForRelease = true; 145 } 146 } 147 } 148 149 152 public void setRollbackOnly() 153 { 154 rollbackOnlyFlag = true; 155 } 156 157 160 public boolean getRollbackOnly() 161 { 162 return rollbackOnlyFlag; 163 } 164 165 private void assertNotRollbackOnly() 166 { 167 if (rollbackOnlyFlag) 168 { 169 throw new JDODataStoreException("Rollback Only !"); 170 } 171 } 172 173 177 public PersistenceBroker getBroker() 178 { 179 if (broker == null) 180 { 181 throw new JDOUserException("No transaction in progress."); 182 } 183 return broker; 184 } 185 186 190 public void setBroker(PersistenceBroker broker) 191 { 192 this.broker = broker; 193 } 194 195 } 196 | Popular Tags |