1 16 17 package org.springframework.orm.ojb; 18 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 import org.apache.ojb.broker.OJBRuntimeException; 22 import org.apache.ojb.broker.PBKey; 23 import org.apache.ojb.broker.PersistenceBroker; 24 import org.apache.ojb.broker.PersistenceBrokerFactory; 25 26 import org.springframework.dao.DataAccessResourceFailureException; 27 import org.springframework.jdbc.datasource.DataSourceUtils; 28 import org.springframework.transaction.support.TransactionSynchronizationAdapter; 29 import org.springframework.transaction.support.TransactionSynchronizationManager; 30 31 44 public abstract class OjbFactoryUtils { 45 46 52 public static final int PERSISTENCE_BROKER_SYNCHRONIZATION_ORDER = 53 DataSourceUtils.CONNECTION_SYNCHRONIZATION_ORDER - 100; 54 55 private static final Log logger = LogFactory.getLog(OjbFactoryUtils.class); 56 57 58 70 public static PersistenceBroker getPersistenceBroker(PBKey pbKey, boolean allowCreate) 71 throws DataAccessResourceFailureException, IllegalStateException { 72 73 PersistenceBrokerHolder pbHolder = 74 (PersistenceBrokerHolder) TransactionSynchronizationManager.getResource(pbKey); 75 if (pbHolder != null) { 76 return pbHolder.getPersistenceBroker(); 77 } 78 79 if (!allowCreate && !TransactionSynchronizationManager.isSynchronizationActive()) { 80 throw new IllegalStateException ("No OJB PersistenceBroker bound to thread, " + 81 "and configuration does not allow creation of non-transactional one here"); 82 } 83 84 try { 85 logger.debug("Opening OJB PersistenceBroker"); 86 PersistenceBroker pb = PersistenceBrokerFactory.createPersistenceBroker(pbKey); 87 88 if (TransactionSynchronizationManager.isSynchronizationActive()) { 89 logger.debug("Registering transaction synchronization for OJB PersistenceBroker"); 90 pbHolder = new PersistenceBrokerHolder(pb); 91 pbHolder.setSynchronizedWithTransaction(true); 92 TransactionSynchronizationManager.registerSynchronization( 93 new PersistenceBrokerSynchronization(pbHolder, pbKey)); 94 TransactionSynchronizationManager.bindResource(pbKey, pbHolder); 95 } 96 97 return pb; 98 } 99 catch (OJBRuntimeException ex) { 100 throw new DataAccessResourceFailureException("Could not open OJB PersistenceBroker", ex); 101 } 102 } 103 104 110 public static void closePersistenceBrokerIfNecessary(PersistenceBroker pb, PBKey pbKey) { 111 releasePersistenceBroker(pb, pbKey); 112 } 113 114 120 public static void releasePersistenceBroker(PersistenceBroker pb, PBKey pbKey) { 121 if (pb == null) { 122 return; 123 } 124 125 PersistenceBrokerHolder pbHolder = 126 (PersistenceBrokerHolder) TransactionSynchronizationManager.getResource(pbKey); 127 if (pbHolder != null && pb == pbHolder.getPersistenceBroker()) { 128 return; 130 } 131 132 logger.debug("Closing OJB PersistenceBroker"); 133 pb.close(); 134 } 135 136 137 142 private static class PersistenceBrokerSynchronization extends TransactionSynchronizationAdapter { 143 144 private final PersistenceBrokerHolder persistenceBrokerHolder; 145 146 private final PBKey pbKey; 147 148 private boolean holderActive = true; 149 150 private PersistenceBrokerSynchronization(PersistenceBrokerHolder pbHolder, PBKey pbKey) { 151 this.persistenceBrokerHolder = pbHolder; 152 this.pbKey = pbKey; 153 } 154 155 public int getOrder() { 156 return PERSISTENCE_BROKER_SYNCHRONIZATION_ORDER; 157 } 158 159 public void suspend() { 160 if (this.holderActive) { 161 TransactionSynchronizationManager.unbindResource(this.pbKey); 162 } 163 } 164 165 public void resume() { 166 if (this.holderActive) { 167 TransactionSynchronizationManager.bindResource(this.pbKey, this.persistenceBrokerHolder); 168 } 169 } 170 171 public void beforeCompletion() { 172 TransactionSynchronizationManager.unbindResource(this.pbKey); 173 this.holderActive = false; 174 releasePersistenceBroker(this.persistenceBrokerHolder.getPersistenceBroker(), this.pbKey); 175 } 176 } 177 178 } 179 | Popular Tags |