KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jdbc > support > lob > LobCreatorUtils


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 JavaDoc;
20 import javax.transaction.TransactionManager JavaDoc;
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.getLog(LobCreatorUtils.class);
43
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, TransactionManager JavaDoc jtaTransactionManager) throws IllegalStateException JavaDoc {
57
58         if (TransactionSynchronizationManager.isSynchronizationActive()) {
59             logger.debug("Registering Spring transaction synchronization for LobCreator");
60             TransactionSynchronizationManager.registerSynchronization(
61                 new SpringLobCreatorSynchronization(lobCreator));
62         }
63         else {
64             if (jtaTransactionManager != null) {
65                 try {
66                     int jtaStatus = jtaTransactionManager.getStatus();
67                     if (jtaStatus == Status.STATUS_ACTIVE || jtaStatus == Status.STATUS_MARKED_ROLLBACK) {
68                         logger.debug("Registering JTA transaction synchronization for LobCreator");
69                         jtaTransactionManager.getTransaction().registerSynchronization(
70                                 new JtaLobCreatorSynchronization(lobCreator));
71                         return;
72                     }
73                 }
74                 catch (Throwable JavaDoc ex) {
75                     throw new TransactionSystemException(
76                             "Could not register synchronization with JTA TransactionManager", ex);
77                 }
78             }
79             throw new IllegalStateException JavaDoc("Active Spring transaction synchronization or active " +
80                 "JTA transaction with specified [javax.transaction.TransactionManager] required");
81         }
82     }
83
84 }
85
Popular Tags