OjbFactoryUtils.java
Upload User: jiancairen
Upload Date: 2007-08-27
Package Size: 26458k
Code Size: 6k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright 2002-2004 the original author or authors.
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package org.springframework.orm.ojb;
  17. import org.apache.ojb.broker.OJBRuntimeException;
  18. import org.apache.ojb.broker.PBKey;
  19. import org.apache.ojb.broker.PersistenceBroker;
  20. import org.apache.ojb.broker.PersistenceBrokerFactory;
  21. import org.springframework.dao.DataAccessResourceFailureException;
  22. import org.springframework.transaction.support.TransactionSynchronizationAdapter;
  23. import org.springframework.transaction.support.TransactionSynchronizationManager;
  24. /**
  25.  * Helper class featuring methods for OJB PersistenceBroker handling,
  26.  * allowing for reuse of PersistenceBroker instances within transactions.
  27.  *
  28.  * <p>Used by PersistenceBrokerTemplate and PersistenceBrokerTransactionManager.
  29.  * Can also be used directly in application code.
  30.  *
  31.  * @author Juergen Hoeller
  32.  * @since 02.07.2004
  33.  * @see PersistenceBrokerTemplate
  34.  * @see PersistenceBrokerTransactionManager
  35.  * @see org.springframework.transaction.jta.JtaTransactionManager
  36.  */
  37. public abstract class OjbFactoryUtils {
  38. /**
  39.  * Get an OJB PersistenceBroker for the given PBKey. Is aware of a
  40.  * corresponding PersistenceBroker bound to the current thread, for
  41.  * example when using PersistenceBrokerTransactionManager. Will
  42.  * create a new PersistenceBroker else, if allowCreate is true.
  43.  * @param pbKey PBKey to create the PersistenceBroker for
  44.  * @param allowCreate if a new PersistenceBroker should be created if no thread-bound found
  45.  * @return the PersistenceBroker
  46.  * @throws DataAccessResourceFailureException if the PersistenceBroker couldn't be created
  47.  * @throws IllegalStateException if no thread-bound PersistenceBroker found and allowCreate false
  48.  */
  49. public static PersistenceBroker getPersistenceBroker(PBKey pbKey, boolean allowCreate)
  50.     throws DataAccessResourceFailureException, IllegalStateException {
  51. return getPersistenceBroker(pbKey, allowCreate, true);
  52. }
  53. /**
  54.  * Get an OJB PersistenceBroker for the given PBKey. Is aware of a
  55.  * corresponding PersistenceBroker bound to the current thread, for
  56.  * example when using PersistenceBrokerTransactionManager. Will
  57.  * create a new PersistenceBroker else.
  58.  * @param pbKey PBKey to create the PersistenceBroker for
  59.  * @param allowCreate if a new PersistenceBroker should be created if no thread-bound found
  60.  * @param allowSynchronization if a new OJB PersistenceBroker is supposed to be
  61.  * registered with transaction synchronization (if synchronization is active).
  62.  * This will always be true for typical data access code.
  63.  * @return the PersistenceBroker
  64.  * @throws DataAccessResourceFailureException if the PersistenceBroker couldn't be created
  65.  * @throws IllegalStateException if no thread-bound PersistenceBroker found and allowCreate false
  66.  */
  67. public static PersistenceBroker getPersistenceBroker(PBKey pbKey, boolean allowCreate,
  68.                                                      boolean allowSynchronization)
  69.     throws DataAccessResourceFailureException, IllegalStateException {
  70. PersistenceBrokerHolder pbHolder =
  71. (PersistenceBrokerHolder) TransactionSynchronizationManager.getResource(pbKey);
  72. if (pbHolder != null) {
  73. return pbHolder.getPersistenceBroker();
  74. }
  75. if (!allowCreate) {
  76. throw new IllegalStateException("No OJB persistence broker bound to thread, and configuration " +
  77. "does not allow creation of new one here");
  78. }
  79. try {
  80. PersistenceBroker pb = PersistenceBrokerFactory.createPersistenceBroker(pbKey);
  81. if (allowSynchronization && TransactionSynchronizationManager.isSynchronizationActive()) {
  82. pbHolder = new PersistenceBrokerHolder(pb);
  83. TransactionSynchronizationManager.bindResource(pbKey, pbHolder);
  84. TransactionSynchronizationManager.registerSynchronization(
  85.     new PersistenceBrokerSynchronization(pbHolder, pbKey));
  86. }
  87. return pb;
  88. }
  89. catch (OJBRuntimeException ex) {
  90. throw new DataAccessResourceFailureException("Could not open OJB persistence broker", ex);
  91. }
  92. }
  93. /**
  94.  * Close the given PersistenceBroker, created for the given PBKey,
  95.  * if it isn't bound to the thread.
  96.  * @param pb PersistenceBroker to close
  97.  * @param pbKey PBKey that the PersistenceBroker was created with
  98.  */
  99. public static void closePersistenceBrokerIfNecessary(PersistenceBroker pb, PBKey pbKey) {
  100. if (pb == null || TransactionSynchronizationManager.hasResource(pbKey)) {
  101. return;
  102. }
  103. pb.close();
  104. }
  105. /**
  106.  * Callback for resource cleanup at the end of a non-OJB transaction
  107.  * (e.g. when participating in a JTA transaction).
  108.  */
  109. private static class PersistenceBrokerSynchronization extends TransactionSynchronizationAdapter {
  110. private final PersistenceBrokerHolder persistenceBrokerHolder;
  111. private final PBKey pbKey;
  112. private PersistenceBrokerSynchronization(PersistenceBrokerHolder pbHolder, PBKey pbKey) {
  113. this.persistenceBrokerHolder = pbHolder;
  114. this.pbKey = pbKey;
  115. }
  116. public void suspend() {
  117. TransactionSynchronizationManager.unbindResource(this.pbKey);
  118. }
  119. public void resume() {
  120. TransactionSynchronizationManager.bindResource(this.pbKey, this.persistenceBrokerHolder);
  121. }
  122. public void beforeCompletion() {
  123. TransactionSynchronizationManager.unbindResource(this.pbKey);
  124. closePersistenceBrokerIfNecessary(this.persistenceBrokerHolder.getPersistenceBroker(), this.pbKey);
  125. }
  126. }
  127. }