spring支持多事务

没有测试过,以前代码记录

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionException;
import org.springframework.transaction.TransactionStatus;

@SuppressWarnings("unchecked")
public class MultiTransactionManager implements PlatformTransactionManager {

	private List transManagerList;

	public void commit(TransactionStatus ts) throws TransactionException {
		TmTsHolder tmts;
		List tmtsList = ((MultiTranscationObject) ts).tmtsList();
		for (Iterator iter = tmtsList.iterator(); iter.hasNext();) {
			tmts = (TmTsHolder) iter.next();
			if (tmts.transactionStatus.isRollbackOnly()) {
				rollback(ts);
				return;
			}
		}

		for (Iterator iter = tmtsList.iterator(); iter.hasNext();) {
			tmts = (TmTsHolder) iter.next();
			tmts.transactionManager.commit(tmts.transactionStatus);
		}
	}

	public TransactionStatus getTransaction(TransactionDefinition def)
			throws TransactionException {
		MultiTranscationObject mo = new MultiTranscationObject();
		for (Iterator it = this.transManagerList.iterator(); it.hasNext();) {
			PlatformTransactionManager tm = (PlatformTransactionManager) it.next();
			mo.addTranscationObject(tm, tm.getTransaction(def));
		}
		return mo;
	}

	public void rollback(TransactionStatus ts) throws TransactionException {
		List tmtsList = ((MultiTranscationObject) ts).tmtsList();
		for (Iterator iter = tmtsList.iterator(); iter.hasNext();) {
			TmTsHolder tmts = (TmTsHolder) iter.next();
			tmts.transactionManager.rollback(tmts.transactionStatus);
		}
	}

	public void setTransManagerList(List transManagerList) {
		this.transManagerList = transManagerList;
	}

	private static class TsSpHolder {
		Object savePoint;
		TransactionStatus transactionStatus;

		TsSpHolder(TransactionStatus ts, Object savePoint) {
			this.transactionStatus = ts;
			this.savePoint = savePoint;
		}
	}

	private static class TmTsHolder {
		PlatformTransactionManager transactionManager;
		TransactionStatus transactionStatus;

		TmTsHolder(PlatformTransactionManager tm, TransactionStatus ts) {
			this.transactionManager = tm;
			this.transactionStatus = ts;
		}
	}

	private static class MultiTranscationObject implements TransactionStatus {
		private boolean rollbackOnly;
		private List tmtsList;

		private MultiTranscationObject() {
			this.rollbackOnly = false;

			this.tmtsList = new ArrayList();
		}

		void addTranscationObject(PlatformTransactionManager tm,
				TransactionStatus ts) {
			this.tmtsList.add(new MultiTransactionManager.TmTsHolder(tm, ts));
		}

		public Object createSavepoint() throws TransactionException {
			List tsspList = new ArrayList();
			for (Iterator it = this.tmtsList.iterator(); it.hasNext();) {
				TransactionStatus ts = ((MultiTransactionManager.TmTsHolder) it
						.next()).transactionStatus;
				tsspList.add(new MultiTransactionManager.TsSpHolder(ts, ts
						.createSavepoint()));
			}

			return tsspList;
		}

		public boolean isNewTransaction() {
			for (Iterator it = this.tmtsList.iterator(); it.hasNext();) {
				TransactionStatus ts = (TransactionStatus) it.next();
				if (!(ts.isNewTransaction()))
					return false;

			}

			return true;
		}

		public boolean isCompleted() {
			return true;
		}

		public boolean hasSavepoint() {
			return true;
		}

		public boolean isRollbackOnly() {
			return this.rollbackOnly;
		}

		public void releaseSavepoint(Object savePoint)
				throws TransactionException {
			List tsspList = (List) savePoint;
			for (Iterator it = tsspList.iterator(); it.hasNext();) {
				MultiTransactionManager.TsSpHolder tssp = (MultiTransactionManager.TsSpHolder) it
						.next();
				tssp.transactionStatus.releaseSavepoint(tssp.savePoint);
			}
		}

		public void rollbackToSavepoint(Object savePoint)
				throws TransactionException {
			List tsspList = (List) savePoint;
			for (Iterator it = tsspList.iterator(); it.hasNext();) {
				MultiTransactionManager.TsSpHolder tssp = (MultiTransactionManager.TsSpHolder) it
						.next();
				tssp.transactionStatus.rollbackToSavepoint(tssp.savePoint);
			}
		}

		public void setRollbackOnly() {
			for (Iterator it = this.tmtsList.iterator(); it.hasNext();) {
				MultiTransactionManager.TmTsHolder tmts = (MultiTransactionManager.TmTsHolder) it
						.next();
				tmts.transactionStatus.setRollbackOnly();
			}
			this.rollbackOnly = true;
		}

		public List tmtsList() {
			return this.tmtsList;
		}
	}
}

 

你可能感兴趣的:(spring)