⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 persistencebrokertransactionmanager.java

📁 spring的源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright 2002-2005 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.orm.ojb;

import java.sql.Connection;

import javax.sql.DataSource;

import org.apache.ojb.broker.OJBRuntimeException;
import org.apache.ojb.broker.PBKey;
import org.apache.ojb.broker.PersistenceBroker;
import org.apache.ojb.broker.PersistenceBrokerFactory;
import org.apache.ojb.broker.TransactionAbortedException;
import org.apache.ojb.broker.accesslayer.LookupException;

import org.springframework.jdbc.datasource.ConnectionHolder;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.jdbc.datasource.JdbcTransactionObjectSupport;
import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;
import org.springframework.transaction.CannotCreateTransactionException;
import org.springframework.transaction.IllegalTransactionStateException;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionSystemException;
import org.springframework.transaction.support.AbstractPlatformTransactionManager;
import org.springframework.transaction.support.DefaultTransactionStatus;
import org.springframework.transaction.support.TransactionSynchronizationManager;

/**
 * PlatformTransactionManager implementation for a single OJB persistence broker key.
 * Binds an OJB PersistenceBroker from the specified key to the thread, potentially
 * allowing for one thread PersistenceBroker per key. OjbFactoryUtils and
 * PersistenceBrokerTemplate are aware of thread-bound persistence brokers and
 * participate in such transactions automatically. Using either is required for
 * OJB access code supporting this transaction management mechanism.
 *
 * <p>This implementation is appropriate for applications that solely use OJB for
 * transactional data access. JTA (usually through JtaTransactionManager) is necessary
 * for accessing multiple transactional resources, in combination with transactional
 * DataSources as connection pools (to be specified in OJB's configuration).
 *
 * @author Juergen Hoeller
 * @since 1.1
 * @see #setJcdAlias
 * @see #setPbKey
 * @see OjbFactoryUtils#getPersistenceBroker
 * @see OjbFactoryUtils#releasePersistenceBroker
 * @see PersistenceBrokerTemplate
 * @see org.springframework.transaction.jta.JtaTransactionManager
 */
public class PersistenceBrokerTransactionManager extends AbstractPlatformTransactionManager {

	private PBKey pbKey = PersistenceBrokerFactory.getDefaultKey();

	private DataSource dataSource;


	/**
	 * Create a new PersistenceBrokerTransactionManager,
	 * sing the default connection configured for OJB.
	 */
	public PersistenceBrokerTransactionManager() {
	}

	/**
	 * Create a new PersistenceBrokerTransactionManager.
	 * @param jcdAlias the JDBC Connection Descriptor alias
	 * of the PersistenceBroker configuration to use
	 */
	public PersistenceBrokerTransactionManager(String jcdAlias) {
		setJcdAlias(jcdAlias);
	}

	/**
	 * Create a new PersistenceBrokerTransactionManager.
	 * @param pbKey the PBKey of the PersistenceBroker configuration to use
	 */
	public PersistenceBrokerTransactionManager(PBKey pbKey) {
		setPbKey(pbKey);
	}

	/**
	 * Set the JDBC Connection Descriptor alias of the PersistenceBroker
	 * configuration to use. Default is the default connection configured for OJB.
	 */
	public void setJcdAlias(String jcdAlias) {
		this.pbKey = new PBKey(jcdAlias);
	}

	/**
	 * Set the PBKey of the PersistenceBroker configuration to use.
	 * Default is the default connection configured for OJB.
	 */
	public void setPbKey(PBKey pbKey) {
		this.pbKey = pbKey;
	}

	/**
	 * Return the PBKey of the PersistenceBroker configuration used.
	 */
	public PBKey getPbKey() {
		return pbKey;
	}

	/**
	 * Set the JDBC DataSource that this instance should manage transactions for.
	 * The DataSource should match the one configured for the OJB JCD alias:
	 * for example, you could specify the same JNDI DataSource for both.
	 * <p>A transactional JDBC Connection for this DataSource will be provided to
	 * application code accessing this DataSource directly via DataSourceUtils
	 * or JdbcTemplate. The Connection will be taken from the Hibernate Session.
	 * <p>The DataSource specified here should be the target DataSource to manage
	 * transactions for, not a TransactionAwareDataSourceProxy. Only data access
	 * code may work with TransactionAwareDataSourceProxy, while the transaction
	 * manager needs to work on the underlying target DataSource. If there's
	 * nevertheless a TransactionAwareDataSourceProxy passed in, it will be
	 * unwrapped to extract its target DataSource.
	 * @see org.springframework.orm.hibernate.LocalDataSourceConnectionProvider
	 * @see org.springframework.orm.hibernate.LocalSessionFactoryBean#setDataSource
	 * @see org.springframework.jdbc.datasource.DataSourceUtils#getConnection
	 * @see org.springframework.jdbc.core.JdbcTemplate
	 * @see org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy
	 */
	public void setDataSource(DataSource dataSource) {
		if (dataSource instanceof TransactionAwareDataSourceProxy) {
			// If we got a TransactionAwareDataSourceProxy, we need to perform transactions
			// for its underlying target DataSource, else data access code won't see
			// properly exposed transactions (i.e. transactions for the target DataSource).
			this.dataSource = ((TransactionAwareDataSourceProxy) dataSource).getTargetDataSource();
		}
		else {
			this.dataSource = dataSource;
		}
	}

	/**
	 * Return the JDBC DataSource that this instance manages transactions for.
	 */
	public DataSource getDataSource() {
		return dataSource;
	}


	protected Object doGetTransaction() {
		PersistenceBrokerTransactionObject txObject = new PersistenceBrokerTransactionObject();
		PersistenceBrokerHolder pbHolder =
		    (PersistenceBrokerHolder) TransactionSynchronizationManager.getResource(getPbKey());
		txObject.setPersistenceBrokerHolder(pbHolder);
		return txObject;
	}

	protected boolean isExistingTransaction(Object transaction) {
		PersistenceBrokerTransactionObject txObject = (PersistenceBrokerTransactionObject) transaction;
		return (txObject.getPersistenceBrokerHolder() != null);
	}

	protected void doBegin(Object transaction, TransactionDefinition definition) {
		if (getDataSource() != null && TransactionSynchronizationManager.hasResource(getDataSource())) {
			throw new IllegalTransactionStateException(
					"Pre-bound JDBC Connection found - PersistenceBrokerTransactionManager does not support " +
					"running within DataSourceTransactionManager if told to manage the DataSource itself. " +
					"It is recommended to use a single PersistenceBrokerTransactionManager for all transactions " +
					"on a single DataSource, no matter whether PersistenceBroker or JDBC access.");
		}

		PersistenceBroker pb = null;

		try {
			pb = getPersistenceBroker();
			if (logger.isDebugEnabled()) {
				logger.debug("Opened new PersistenceBroker [" + pb + "] for OJB transaction");
			}

			PersistenceBrokerTransactionObject txObject = (PersistenceBrokerTransactionObject) transaction;
			txObject.setPersistenceBrokerHolder(new PersistenceBrokerHolder(pb));

			Connection con = pb.serviceConnectionManager().getConnection();
			Integer previousIsolationLevel = DataSourceUtils.prepareConnectionForTransaction(con, definition);
			txObject.setPreviousIsolationLevel(previousIsolationLevel);

			pb.beginTransaction();

			// Register the OJB PersistenceBroker's JDBC Connection for the DataSource, if set.
			if (getDataSource() != null) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -