blob: 4c1163d1eb4929976a68c9d5d34d562abecf937e (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
/*******************************************************************************
* Copyright (c) 2004, 2007 Boeing.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Boeing - initial API and implementation
*******************************************************************************/
package org.eclipse.osee.framework.database.core;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
/**
* This abstract class provides a uniform way of executing database transactions. It handles exceptions ensuring that
* transactions are processed in the correct order and roll-backs are performed whenever errors are detected.
*
* @author Roberto E. Escobar
*/
public abstract class DbTransaction {
/**
* Gets the name of this transaction. This is provided mainly for logging purposes.
*
* @return String transaction class Name
*/
protected String getTxName() {
return this.getClass().getCanonicalName();
}
/**
* This template method calls {@link #handleTxWork} which is provided by child classes. This method handles
* roll-backs and exception handling to prevent transactions from being left in an incorrect state.
*
* @throws OseeCoreException
*/
public void execute() throws OseeCoreException {
DatabaseTransactions.execute(new InternalTransactionWork());
}
/**
* Provides the transaction's work implementation.
*
* @param connection
* @throws OseeCoreException
*/
protected abstract void handleTxWork(OseeConnection connection) throws OseeCoreException;
/**
* When an exception is detected during transaction processing, the exception is caught and passed to this method.
* This convenience method is provided so child classes have access to the exception. <br/>
* <b>Override to handle transaction exception</b>
*
* @param ex
* @throws Exception
*/
protected void handleTxException(Exception ex) {
}
/**
* This convenience method is provided in case child classes have a portion of code that needs to execute always at
* the end of the transaction, regardless of exceptions. <br/>
* <b>Override to add additional code to finally block</b>
*
* @throws OseeCoreException
*/
protected void handleTxFinally() throws OseeCoreException {
// override to add additional code to finally
}
private final class InternalTransactionWork implements IDbTransactionWork {
@Override
public void handleTxException(Exception ex) {
DbTransaction.this.handleTxException(ex);
}
@Override
public void handleTxFinally() throws OseeCoreException {
DbTransaction.this.handleTxFinally();
}
@Override
public void handleTxWork(OseeConnection connection) throws OseeCoreException {
DbTransaction.this.handleTxWork(connection);
}
@Override
public String getName() {
return DbTransaction.this.getTxName();
}
};
}
|