ozeigermann 2004/07/01 05:56:15
Added: src/share/org/apache/slide/transaction
AbstractXAResource.java TransactionalResource.java
Log:
Temporarily introduced abstract XA Resource for many sevices to base upon.
Will be replaced by clases from commons transaction component soon
Revision Changes Path
1.1
jakarta-slide/src/share/org/apache/slide/transaction/AbstractXAResource.java
Index: AbstractXAResource.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-slide/src/share/org/apache/slide/transaction/AbstractXAResource.java,v
1.1 2004/07/01 12:56:15 ozeigermann Exp $
* $Revision: 1.1 $
* $Date: 2004/07/01 12:56:15 $
*
* ====================================================================
*
* Copyright 2004 The Apache Software Foundation
*
* 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.apache.slide.transaction;
import java.util.HashMap;
import java.util.Map;
import javax.transaction.Status;
import javax.transaction.xa.XAException;
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;
import org.apache.slide.util.XidWrapper;
import org.apache.slide.util.logger.LoggerFacade;
/**
* Abstract XAResource doing all the tedious tasks shared by many XAResouce
implementations.
*/
public abstract class AbstractXAResource implements XAResource, Status {
// there might be at least one active transaction branch per thread
private ThreadLocal activeTransactionBranch = new ThreadLocal();
private Map suspendedContexts = new HashMap();
private Map activeContexts = new HashMap();
public abstract boolean isSameRM(XAResource xares) throws XAException;
public abstract Xid[] recover(int flag) throws XAException;
protected abstract LoggerFacade getLoggerFacade();
public void forget(Xid xid) throws XAException {
getLoggerFacade().logFine("Preparing transaction branch " + xid);
TransactionalResource ts = getTransactionalResource(xid);
if (ts == null) {
throw new XAException(XAException.XAER_NOTA);
}
ts.close();
setCurrentlyActiveTransactionalResource(null);
removeActiveTransactionalResource(xid);
}
public void commit(Xid xid, boolean onePhase) throws XAException {
TransactionalResource ts = getTransactionalResource(xid);
if (ts == null) {
throw new XAException(XAException.XAER_NOTA);
}
getLoggerFacade().logFine("Committing transaction branch " + ts);
if (ts.getStatus() == STATUS_MARKED_ROLLBACK) {
throw new XAException(XAException.XA_RBROLLBACK);
}
if (ts.getStatus() != STATUS_PREPARED) {
if (onePhase) {
ts.prepare();
} else {
throw new XAException(XAException.XAER_PROTO);
}
}
ts.commit();
setCurrentlyActiveTransactionalResource(null);
removeActiveTransactionalResource(xid);
}
public void rollback(Xid xid) throws XAException {
TransactionalResource ts = getTransactionalResource(xid);
if (ts == null) {
throw new XAException(XAException.XAER_NOTA);
}
getLoggerFacade().logFine("Rolling back transaction branch " + ts);
ts.rollback();
setCurrentlyActiveTransactionalResource(null);
removeActiveTransactionalResource(xid);
}
public int prepare(Xid xid) throws XAException {
TransactionalResource ts = getTransactionalResource(xid);
if (ts == null) {
throw new XAException(XAException.XAER_NOTA);
}
getLoggerFacade().logFine("Preparing transaction branch " + ts);
if (ts.getStatus() == STATUS_MARKED_ROLLBACK) {
throw new XAException(XAException.XA_RBROLLBACK);
}
int result = ts.prepare();
ts.setStatus(STATUS_PREPARED);
return result;
}
public void end(Xid xid, int flags) throws XAException {
TransactionalResource ts = getActiveTransactionalResource(xid);
if (ts == null) {
throw new XAException(XAException.XAER_NOTA);
}
if (getCurrentlyActiveTransactionalResource() == null) {
throw new XAException(XAException.XAER_INVAL);
}
getLoggerFacade().logFine(
"Thread "
+ Thread.currentThread()
+ (flags == TMSUSPEND ? " suspends" : flags == TMFAIL ? " fails" : "
ends")
+ " work on behalf of transaction branch "
+ ts);
switch (flags) {
case TMSUSPEND :
addSuspendedTransactionalResource(xid, ts);
removeActiveTransactionalResource(xid);
break;
case TMFAIL :
ts.setStatus(STATUS_MARKED_ROLLBACK);
break;
case TMSUCCESS :
break;
}
setCurrentlyActiveTransactionalResource(null);
}
public void start(Xid xid, int flags) throws XAException {
if (getCurrentlyActiveTransactionalResource() != null) {
throw new XAException(XAException.XAER_INVAL);
}
getLoggerFacade().logFine(
"Thread "
+ Thread.currentThread()
+ (flags == TMNOFLAGS ? " starts" : flags == TMJOIN ? " joins" : "
resumes")
+ " work on behalf of transaction branch "
+ xid);
TransactionalResource ts;
switch (flags) {
// a new transaction
case TMNOFLAGS :
case TMJOIN :
default :
try {
ts = createTransactionResource(xid);
} catch (Exception e) {
getLoggerFacade().logSevere("Could not create
new transactional resource", e);
throw new XAException(e.getMessage());
}
break;
case TMRESUME :
ts = getSuspendedTransactionalResource(xid);
if (ts == null) {
throw new XAException(XAException.XAER_NOTA);
}
removeSuspendedTransactionalResource(xid);
break;
}
setCurrentlyActiveTransactionalResource(ts);
addAcitveTransactionalResource(xid, ts);
}
abstract protected TransactionalResource createTransactionResource(Xid xid)
throws Exception;
protected TransactionalResource getCurrentlyActiveTransactionalResource() {
TransactionalResource context = (TransactionalResource)
activeTransactionBranch.get();
return context;
}
protected void setCurrentlyActiveTransactionalResource(TransactionalResource
context) {
activeTransactionBranch.set(context);
}
protected TransactionalResource getTransactionalResource(Xid xid) {
TransactionalResource ts = getActiveTransactionalResource(xid);
if (ts != null) return ts;
else return getSuspendedTransactionalResource(xid);
}
protected TransactionalResource getActiveTransactionalResource(Xid xid) {
Xid wxid = XidWrapper.wrap(xid);
return (TransactionalResource) activeContexts.get(wxid);
}
protected TransactionalResource getSuspendedTransactionalResource(Xid xid) {
Xid wxid = XidWrapper.wrap(xid);
return (TransactionalResource) suspendedContexts.get(wxid);
}
protected void addAcitveTransactionalResource(Xid xid, TransactionalResource
txContext) {
Xid wxid = XidWrapper.wrap(xid);
activeContexts.put(wxid, txContext);
}
protected void addSuspendedTransactionalResource(Xid xid, TransactionalResource
txContext) {
Xid wxid = XidWrapper.wrap(xid);
suspendedContexts.put(wxid, txContext);
}
protected void removeActiveTransactionalResource(Xid xid) {
Xid wxid = XidWrapper.wrap(xid);
activeContexts.remove(wxid);
}
protected void removeSuspendedTransactionalResource(Xid xid) {
Xid wxid = XidWrapper.wrap(xid);
suspendedContexts.remove(wxid);
}
}
1.1
jakarta-slide/src/share/org/apache/slide/transaction/TransactionalResource.java
Index: TransactionalResource.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-slide/src/share/org/apache/slide/transaction/TransactionalResource.java,v
1.1 2004/07/01 12:56:15 ozeigermann Exp $
* $Revision: 1.1 $
* $Date: 2004/07/01 12:56:15 $
*
* ====================================================================
*
* Copyright 2004 The Apache Software Foundation
*
* 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.apache.slide.transaction;
import javax.transaction.xa.XAException;
import javax.transaction.xa.Xid;
/**
* Interface for something that makes up a transactional resource.
*/
public interface TransactionalResource {
public void commit() throws XAException;
public int prepare() throws XAException;
public void rollback() throws XAException;
public void close() throws XAException;
public int getStatus();
public void setStatus(int status);
public Xid getXid();
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]