KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.springframework.jdbc.datasource.DataSourceUtils;
20 import org.springframework.transaction.support.TransactionSynchronizationAdapter;
21 import org.springframework.util.Assert;
22
23 /**
24  * Callback for resource cleanup at the end of a Spring transaction.
25  * Invokes <code>LobCreator.close()</code> to clean up temporary LOBs
26  * that might have been created.
27  *
28  * @author Juergen Hoeller
29  * @since 2.0
30  * @see org.springframework.jdbc.support.lob.LobCreator#close()
31  */

32 public class SpringLobCreatorSynchronization extends TransactionSynchronizationAdapter {
33
34     /**
35      * Order value for TransactionSynchronization objects that clean up LobCreators.
36      * Return CONNECTION_SYNCHRONIZATION_ORDER - 200 to execute LobCreator cleanup
37      * before Hibernate Session (- 100) and JDBC Connection cleanup, if any.
38      * @see org.springframework.jdbc.datasource.DataSourceUtils#CONNECTION_SYNCHRONIZATION_ORDER
39      */

40     public static final int LOB_CREATOR_SYNCHRONIZATION_ORDER =
41             DataSourceUtils.CONNECTION_SYNCHRONIZATION_ORDER - 200;
42
43
44     private final LobCreator lobCreator;
45
46     private boolean beforeCompletionCalled = false;
47
48
49     /**
50      * Create a SpringLobCreatorSynchronization for the given LobCreator.
51      * @param lobCreator the LobCreator to close after transaction completion
52      */

53     public SpringLobCreatorSynchronization(LobCreator lobCreator) {
54         Assert.notNull(lobCreator, "LobCreator must not be null");
55         this.lobCreator = lobCreator;
56     }
57
58     public int getOrder() {
59         return LOB_CREATOR_SYNCHRONIZATION_ORDER;
60     }
61
62
63     public void beforeCompletion() {
64         // Close the LobCreator early if possible, to avoid issues with strict JTA
65
// implementations that issue warnings when doing JDBC operations after
66
// transaction completion.
67
this.beforeCompletionCalled = true;
68         this.lobCreator.close();
69     }
70
71     public void afterCompletion(int status) {
72         if (!this.beforeCompletionCalled) {
73             // beforeCompletion not called before (probably because of flushing on commit
74
// in the transaction manager, after the chain of beforeCompletion calls).
75
// Close the LobCreator here.
76
this.lobCreator.close();
77         }
78     }
79
80 }
81
Popular Tags