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.Synchronization;
20:
21: import org.springframework.util.Assert;
22:
23: /**
24: * Callback for resource cleanup at the end of a JTA 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: * @see javax.transaction.Transaction#registerSynchronization
32: */
33: public class JtaLobCreatorSynchronization implements Synchronization {
34:
35: private final LobCreator lobCreator;
36:
37: private boolean beforeCompletionCalled = false;
38:
39: /**
40: * Create a JtaLobCreatorSynchronization for the given LobCreator.
41: * @param lobCreator the LobCreator to close after transaction completion
42: */
43: public JtaLobCreatorSynchronization(LobCreator lobCreator) {
44: Assert.notNull(lobCreator, "LobCreator must not be null");
45: this .lobCreator = lobCreator;
46: }
47:
48: public void beforeCompletion() {
49: // Close the LobCreator early if possible, to avoid issues with strict JTA
50: // implementations that issue warnings when doing JDBC operations after
51: // transaction completion.
52: this .beforeCompletionCalled = true;
53: this .lobCreator.close();
54: }
55:
56: public void afterCompletion(int status) {
57: if (!this .beforeCompletionCalled) {
58: // beforeCompletion not called before (probably because of JTA rollback).
59: // Close the LobCreator here.
60: this.lobCreator.close();
61: }
62: }
63:
64: }
|