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 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
33: TransactionSynchronizationAdapter {
34:
35: /**
36: * Order value for TransactionSynchronization objects that clean up LobCreators.
37: * Return CONNECTION_SYNCHRONIZATION_ORDER - 200 to execute LobCreator cleanup
38: * before Hibernate Session (- 100) and JDBC Connection cleanup, if any.
39: * @see org.springframework.jdbc.datasource.DataSourceUtils#CONNECTION_SYNCHRONIZATION_ORDER
40: */
41: public static final int LOB_CREATOR_SYNCHRONIZATION_ORDER = DataSourceUtils.CONNECTION_SYNCHRONIZATION_ORDER - 200;
42:
43: private final LobCreator lobCreator;
44:
45: private boolean beforeCompletionCalled = false;
46:
47: /**
48: * Create a SpringLobCreatorSynchronization for the given LobCreator.
49: * @param lobCreator the LobCreator to close after transaction completion
50: */
51: public SpringLobCreatorSynchronization(LobCreator lobCreator) {
52: Assert.notNull(lobCreator, "LobCreator must not be null");
53: this .lobCreator = lobCreator;
54: }
55:
56: public int getOrder() {
57: return LOB_CREATOR_SYNCHRONIZATION_ORDER;
58: }
59:
60: public void beforeCompletion() {
61: // Close the LobCreator early if possible, to avoid issues with strict JTA
62: // implementations that issue warnings when doing JDBC operations after
63: // transaction completion.
64: this .beforeCompletionCalled = true;
65: this .lobCreator.close();
66: }
67:
68: public void afterCompletion(int status) {
69: if (!this .beforeCompletionCalled) {
70: // beforeCompletion not called before (probably because of flushing on commit
71: // in the transaction manager, after the chain of beforeCompletion calls).
72: // Close the LobCreator here.
73: this.lobCreator.close();
74: }
75: }
76:
77: }
|