01: /*
02: * Copyright 2002-2006 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.jdbc.support.lob;
18:
19: import javax.transaction.Status;
20: import javax.transaction.TransactionManager;
21:
22: import org.apache.commons.logging.Log;
23: import org.apache.commons.logging.LogFactory;
24:
25: import org.springframework.transaction.TransactionSystemException;
26: import org.springframework.transaction.support.TransactionSynchronizationManager;
27:
28: /**
29: * Helper class for registering a transaction synchronization for closing
30: * a LobCreator, preferring Spring transaction synchronization and falling
31: * back to plain JTA transaction synchronization.
32: *
33: * @author Juergen Hoeller
34: * @since 2.0
35: * @see SpringLobCreatorSynchronization
36: * @see org.springframework.transaction.support.TransactionSynchronizationManager
37: * @see JtaLobCreatorSynchronization
38: * @see javax.transaction.Transaction#registerSynchronization
39: */
40: public abstract class LobCreatorUtils {
41:
42: private static final Log logger = LogFactory
43: .getLog(LobCreatorUtils.class);
44:
45: /**
46: * Register a transaction synchronization for closing the given LobCreator,
47: * preferring Spring transaction synchronization and falling back to
48: * plain JTA transaction synchronization.
49: * @param lobCreator the LobCreator to close after transaction completion
50: * @param jtaTransactionManager the JTA TransactionManager to fall back to
51: * when no Spring transaction synchronization is active (may be <code>null</code>)
52: * @throws IllegalStateException if there is neither active Spring transaction
53: * synchronization nor active JTA transaction synchronization
54: */
55: public static void registerTransactionSynchronization(
56: LobCreator lobCreator,
57: TransactionManager jtaTransactionManager)
58: throws IllegalStateException {
59:
60: if (TransactionSynchronizationManager.isSynchronizationActive()) {
61: logger
62: .debug("Registering Spring transaction synchronization for LobCreator");
63: TransactionSynchronizationManager
64: .registerSynchronization(new SpringLobCreatorSynchronization(
65: lobCreator));
66: } else {
67: if (jtaTransactionManager != null) {
68: try {
69: int jtaStatus = jtaTransactionManager.getStatus();
70: if (jtaStatus == Status.STATUS_ACTIVE
71: || jtaStatus == Status.STATUS_MARKED_ROLLBACK) {
72: logger
73: .debug("Registering JTA transaction synchronization for LobCreator");
74: jtaTransactionManager
75: .getTransaction()
76: .registerSynchronization(
77: new JtaLobCreatorSynchronization(
78: lobCreator));
79: return;
80: }
81: } catch (Throwable ex) {
82: throw new TransactionSystemException(
83: "Could not register synchronization with JTA TransactionManager",
84: ex);
85: }
86: }
87: throw new IllegalStateException(
88: "Active Spring transaction synchronization or active "
89: + "JTA transaction with specified [javax.transaction.TransactionManager] required");
90: }
91: }
92:
93: }
|