svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.db / org.gvsig.fmap.dal.db.jdbc / src / main / java / org / gvsig / fmap / dal / store / jdbc2 / spi / operations / AbstractConnectionOperation.java @ 44198
History | View | Annotate | Download (5 KB)
1 |
package org.gvsig.fmap.dal.store.jdbc2.spi.operations; |
---|---|
2 |
|
3 |
import java.sql.Connection; |
4 |
import java.sql.SQLException; |
5 |
import org.gvsig.fmap.dal.resource.exception.AccessResourceException; |
6 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCTransactionRollbackException; |
7 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
8 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
9 |
import org.slf4j.Logger; |
10 |
import org.slf4j.LoggerFactory; |
11 |
|
12 |
@SuppressWarnings("UseSpecificCatch") |
13 |
public abstract class AbstractConnectionOperation implements ConnectionOperation { |
14 |
|
15 |
final static protected Logger LOGGER = LoggerFactory.getLogger(AbstractConnectionOperation.class); |
16 |
|
17 |
final protected JDBCHelper helper; |
18 |
|
19 |
protected Connection conn = null; |
20 |
|
21 |
public AbstractConnectionOperation(JDBCHelper helper) {
|
22 |
this.helper = helper;
|
23 |
} |
24 |
|
25 |
@Override
|
26 |
public boolean continueTransactionAllowed() { |
27 |
return false; |
28 |
} |
29 |
|
30 |
@Override
|
31 |
public boolean needTransaction() { |
32 |
return true; |
33 |
} |
34 |
|
35 |
protected JDBCSQLBuilderBase createSQLBuilder() {
|
36 |
return this.helper.createSQLBuilder(); |
37 |
} |
38 |
|
39 |
@Override
|
40 |
public Object perform() { |
41 |
try {
|
42 |
if( this.helper.isThreadSafe() ) { |
43 |
return this.perform_operation(); |
44 |
} |
45 |
synchronized(AbstractConnectionOperation.class) {
|
46 |
return this.perform_operation(); |
47 |
} |
48 |
} catch (RuntimeException ex) { |
49 |
throw ex;
|
50 |
} catch (Exception ex) { |
51 |
throw new RuntimeException("Can't perform operation '"+this.getClass().getSimpleName()+"'.",ex); |
52 |
} |
53 |
} |
54 |
|
55 |
protected Connection getConnection() throws AccessResourceException { |
56 |
if( this.conn == null ) { |
57 |
this.conn = this.helper.getConnection(); |
58 |
} |
59 |
return this.conn; |
60 |
} |
61 |
|
62 |
protected Object perform_operation() throws Exception { |
63 |
Object result = null; |
64 |
LOGGER.debug("preparing execution of "+this.getClass().getSimpleName()+"."); |
65 |
if( needTransaction() ) {
|
66 |
try {
|
67 |
if( this.helper.allowNestedOperations() ) { |
68 |
boolean commitOnFinish = true; |
69 |
if (!this.getConnection().getAutoCommit()) { |
70 |
// Casi seguro que es una operacion anidada, asi que no
|
71 |
// terminamos la transaccion.
|
72 |
commitOnFinish = false;
|
73 |
} |
74 |
this.getConnection().setAutoCommit(false); |
75 |
try {
|
76 |
LOGGER.debug("Excuting operation {}.", this.getClass().getSimpleName()); |
77 |
result = perform(this.getConnection());
|
78 |
} catch (Exception ex) { |
79 |
try {
|
80 |
this.getConnection().rollback();
|
81 |
} catch (Exception e1) { |
82 |
throw new JDBCTransactionRollbackException(e1, ex); |
83 |
} |
84 |
throw new RuntimeException("Can't perform operation '"+this.getClass().getSimpleName()+"'.",ex); |
85 |
} |
86 |
if( commitOnFinish ) {
|
87 |
this.getConnection().commit();
|
88 |
this.getConnection().setAutoCommit(true); |
89 |
} |
90 |
} else {
|
91 |
if (!this.getConnection().getAutoCommit()) { |
92 |
// Casi seguro que es una transaccion anidada, si no
|
93 |
// esta permitido, petamos.
|
94 |
if (!continueTransactionAllowed()) {
|
95 |
throw new SQLException("nested operations not allowed."); |
96 |
} |
97 |
} |
98 |
this.getConnection().setAutoCommit(false); |
99 |
try {
|
100 |
LOGGER.debug("Excuting operation {}.", this.getClass().getSimpleName()); |
101 |
result = perform(this.getConnection());
|
102 |
} catch (Exception ex) { |
103 |
try {
|
104 |
this.getConnection().rollback();
|
105 |
} catch (Exception e1) { |
106 |
throw new JDBCTransactionRollbackException(e1, ex); |
107 |
} |
108 |
throw new RuntimeException("Can't perform operation '"+this.getClass().getSimpleName()+"'.",ex); |
109 |
} |
110 |
this.getConnection().commit();
|
111 |
} |
112 |
} finally {
|
113 |
helper.closeConnection(this.getConnection());
|
114 |
conn = null;
|
115 |
} |
116 |
} else {
|
117 |
try {
|
118 |
LOGGER.debug("Excuting operation {}.", this.getClass().getSimpleName()); |
119 |
result = perform(this.getConnection());
|
120 |
} catch (Exception ex) { |
121 |
throw new RuntimeException("Can't perform operation '"+this.getClass().getSimpleName()+"'.",ex); |
122 |
} finally {
|
123 |
helper.closeConnection(this.getConnection());
|
124 |
conn = null;
|
125 |
} |
126 |
} |
127 |
return result;
|
128 |
} |
129 |
} |