01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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:
17: package org.springframework.transaction.jta;
18:
19: import javax.transaction.NotSupportedException;
20: import javax.transaction.SystemException;
21: import javax.transaction.Transaction;
22: import javax.transaction.TransactionManager;
23:
24: import org.springframework.util.Assert;
25:
26: /**
27: * Default implementation of the {@link TransactionFactory} strategy interface,
28: * simply wrapping a standard JTA {@link javax.transaction.TransactionManager}.
29: *
30: * <p>Does not support transaction names; simply ignores any specified name.
31: *
32: * @author Juergen Hoeller
33: * @since 2.5
34: * @see javax.transaction.TransactionManager#setTransactionTimeout(int)
35: * @see javax.transaction.TransactionManager#begin()
36: * @see javax.transaction.TransactionManager#getTransaction()
37: */
38: public class SimpleTransactionFactory implements TransactionFactory {
39:
40: private final TransactionManager transactionManager;
41:
42: /**
43: * Create a new SimpleTransactionFactory for the given TransactionManager
44: * @param transactionManager the JTA TransactionManager to wrap
45: */
46: public SimpleTransactionFactory(
47: TransactionManager transactionManager) {
48: Assert.notNull(transactionManager,
49: "TransactionManager must not be null");
50: this .transactionManager = transactionManager;
51: }
52:
53: public Transaction createTransaction(String name, int timeout)
54: throws NotSupportedException, SystemException {
55: if (timeout >= 0) {
56: this.transactionManager.setTransactionTimeout(timeout);
57: }
58: this.transactionManager.begin();
59: return this.transactionManager.getTransaction();
60: }
61:
62: }
|