All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.commons.transaction.AbstractTransactionalResourceManager Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.apache.commons.transaction;

import java.util.concurrent.TimeUnit;

import org.apache.commons.transaction.locking.LockException;
import org.apache.commons.transaction.locking.LockManager;

/**
 * Abstract base class for transactional resource managers.
 * 

* This implementation takes care of most administrative tasks a transactional * resource manager has to perform. Sublcass * {@link AbstractTransactionalResourceManager.AbstractTxContext} to hold all * information necessary for each transaction. Additionally, you have to * implement {@link #createContext()} to create an object of that type, and * {@link #commitCanFail()}. * *

* This implementation is thread-safe. */ public abstract class AbstractTransactionalResourceManager implements ManageableResourceManager { protected ThreadLocal activeTx = new ThreadLocal(); private LockManager lm; private String name; private boolean partofComplexTransaction = false; protected abstract T createContext(); public AbstractTransactionalResourceManager(String name) { this.name = name; } // can be used to share a lock manager with other transactional resource // managers public AbstractTransactionalResourceManager(String name, LockManager lm) { this.name = name; this.lm = lm; } @Override public boolean isRollbackOnly() { T txContext = getCheckedActiveTx(); return (txContext.isMarkedForRollback()); } @Override public void startTransaction(long timeout, TimeUnit unit) { if (getActiveTx() != null) { throw new IllegalStateException("Active thread " + Thread.currentThread() + " already associated with a transaction!"); } T txContext = createContext(); txContext.start(timeout, unit); setActiveTx(txContext); } @Override public void rollbackTransaction() { T txContext = getCheckedActiveTx(); txContext.rollback(); forgetTransaction(); } @Override public void forgetTransaction() { T txContext = getCheckedActiveTx(); txContext.dispose(); setActiveTx(null); partofComplexTransaction = false; } @Override public boolean commitTransaction() { T txContext = getCheckedActiveTx(); if (txContext.isMarkedForRollback()) { throw new IllegalStateException("Active thread " + Thread.currentThread() + " is marked for rollback!"); } txContext.commit(); forgetTransaction(); return true; } protected T getActiveTx() { return activeTx.get(); } protected T getCheckedActiveTx() { T txContext = getActiveTx(); if (txContext == null) { throw new IllegalStateException("Active thread " + Thread.currentThread() + " not associated with a transaction!"); } return txContext; } protected void setActiveTx(T txContext) { activeTx.set(txContext); } public boolean isReadOnly() { T txContext = getCheckedActiveTx(); return (txContext.isReadOnly()); } public abstract class AbstractTxContext { private boolean readOnly = true; private boolean markedForRollback = false; public void join() { } public void start(long timeout, TimeUnit unit) { getLm().startWork(timeout, unit); } public boolean isReadOnly() { return readOnly; } public void setReadOnly(boolean readOnly) { this.readOnly = readOnly; } public void readLock(Object id) throws LockException { getLm().lock(getName(), id, false); } public void writeLock(Object id) throws LockException { getLm().lock(getName(), id, true); } public boolean isMarkedForRollback() { return markedForRollback; } public void markForRollback() { markedForRollback = true; } public void dispose() { if (!isPartofComplexTransaction()) getLm().endWork(); } public void commit() { } public void rollback() { } public boolean prepare() { return !isMarkedForRollback(); } } protected LockManager getLm() { return lm; } public void setLm(LockManager lm) { this.lm = lm; } public String getName() { return name; } public void setName(String name) { this.name = name; } public abstract boolean commitCanFail(); @Override public void joinTransaction(LockManager lm) { if (getActiveTx() != null) { throw new IllegalStateException("Active thread " + Thread.currentThread() + " already associated with a transaction!"); } setLm(lm); T txContext = createContext(); txContext.join(); setActiveTx(txContext); partofComplexTransaction = true; } @Override public boolean prepareTransaction() { T txContext = getCheckedActiveTx(); return txContext.prepare(); } public boolean isPartofComplexTransaction() { return partofComplexTransaction; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy