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 @ 45065
History | View | Annotate | Download (5.94 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2020 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.fmap.dal.store.jdbc2.spi.operations; |
25 |
|
26 |
import java.sql.Connection; |
27 |
import java.sql.SQLException; |
28 |
import org.gvsig.fmap.dal.resource.exception.AccessResourceException; |
29 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCTransactionRollbackException; |
30 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
31 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
32 |
import org.slf4j.Logger; |
33 |
import org.slf4j.LoggerFactory; |
34 |
|
35 |
@SuppressWarnings("UseSpecificCatch") |
36 |
public abstract class AbstractConnectionOperation implements ConnectionOperation { |
37 |
|
38 |
final static protected Logger LOGGER = LoggerFactory.getLogger(AbstractConnectionOperation.class); |
39 |
|
40 |
final protected JDBCHelper helper; |
41 |
|
42 |
protected Connection conn = null; |
43 |
|
44 |
public AbstractConnectionOperation(JDBCHelper helper) {
|
45 |
this.helper = helper;
|
46 |
} |
47 |
|
48 |
@Override
|
49 |
public boolean continueTransactionAllowed() { |
50 |
return false; |
51 |
} |
52 |
|
53 |
@Override
|
54 |
public boolean needTransaction() { |
55 |
return true; |
56 |
} |
57 |
|
58 |
protected JDBCSQLBuilderBase createSQLBuilder() {
|
59 |
return this.helper.createSQLBuilder(); |
60 |
} |
61 |
|
62 |
@Override
|
63 |
public Object perform() { |
64 |
try {
|
65 |
if( this.helper.isThreadSafe() ) { |
66 |
return this.perform_operation(); |
67 |
} |
68 |
synchronized(AbstractConnectionOperation.class) {
|
69 |
return this.perform_operation(); |
70 |
} |
71 |
} catch (RuntimeException ex) { |
72 |
throw ex;
|
73 |
} catch (Exception ex) { |
74 |
throw new RuntimeException("Can't perform operation '"+this.getClass().getSimpleName()+"'.",ex); |
75 |
} |
76 |
} |
77 |
|
78 |
protected Connection getConnection() throws AccessResourceException { |
79 |
if( this.conn == null ) { |
80 |
this.conn = this.helper.getConnection(); |
81 |
} |
82 |
return this.conn; |
83 |
} |
84 |
|
85 |
protected Object perform_operation() throws Exception { |
86 |
Object result = null; |
87 |
LOGGER.trace("preparing execution of "+this.getClass().getSimpleName()+"."); |
88 |
if( needTransaction() ) {
|
89 |
try {
|
90 |
if( this.helper.allowNestedOperations() ) { |
91 |
boolean commitOnFinish = true; |
92 |
if (!this.getConnection().getAutoCommit()) { |
93 |
// Casi seguro que es una operacion anidada, asi que no
|
94 |
// terminamos la transaccion.
|
95 |
commitOnFinish = false;
|
96 |
} |
97 |
this.getConnection().setAutoCommit(false); |
98 |
try {
|
99 |
LOGGER.trace("Excuting operation {}.", this.getClass().getSimpleName()); |
100 |
result = perform(this.getConnection());
|
101 |
} catch (Exception ex) { |
102 |
try {
|
103 |
this.getConnection().rollback();
|
104 |
} catch (Exception e1) { |
105 |
throw new JDBCTransactionRollbackException(e1, ex); |
106 |
} |
107 |
throw new RuntimeException("Can't perform operation '"+this.getClass().getSimpleName()+"'.",ex); |
108 |
} |
109 |
if( commitOnFinish ) {
|
110 |
this.getConnection().commit();
|
111 |
this.getConnection().setAutoCommit(true); |
112 |
} |
113 |
} else {
|
114 |
if (!this.getConnection().getAutoCommit()) { |
115 |
// Casi seguro que es una transaccion anidada, si no
|
116 |
// esta permitido, petamos.
|
117 |
if (!continueTransactionAllowed()) {
|
118 |
throw new SQLException("nested operations not allowed."); |
119 |
} |
120 |
} |
121 |
this.getConnection().setAutoCommit(false); |
122 |
try {
|
123 |
LOGGER.trace("Excuting operation {}.", this.getClass().getSimpleName()); |
124 |
result = perform(this.getConnection());
|
125 |
} catch (Exception ex) { |
126 |
try {
|
127 |
this.getConnection().rollback();
|
128 |
} catch (Exception e1) { |
129 |
throw new JDBCTransactionRollbackException(e1, ex); |
130 |
} |
131 |
throw new RuntimeException("Can't perform operation '"+this.getClass().getSimpleName()+"'.",ex); |
132 |
} |
133 |
this.getConnection().commit();
|
134 |
} |
135 |
} finally {
|
136 |
helper.closeConnection(this.getConnection());
|
137 |
conn = null;
|
138 |
} |
139 |
} else {
|
140 |
try {
|
141 |
LOGGER.trace("Excuting operation {}.", this.getClass().getSimpleName()); |
142 |
result = perform(this.getConnection());
|
143 |
} catch (Exception ex) { |
144 |
throw new RuntimeException("Can't perform operation '"+this.getClass().getSimpleName()+"'.",ex); |
145 |
} finally {
|
146 |
helper.closeConnection(this.getConnection());
|
147 |
conn = null;
|
148 |
} |
149 |
} |
150 |
return result;
|
151 |
} |
152 |
} |