Statistics
| Revision:

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 @ 45534

History | View | Annotate | Download (6.11 KB)

1 45065 jjdelcerro
/**
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 43020 jjdelcerro
package org.gvsig.fmap.dal.store.jdbc2.spi.operations;
25
26
import java.sql.Connection;
27
import java.sql.SQLException;
28 43377 jjdelcerro
import org.gvsig.fmap.dal.resource.exception.AccessResourceException;
29 43020 jjdelcerro
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 44191 jjdelcerro
@SuppressWarnings("UseSpecificCatch")
36 43020 jjdelcerro
public abstract class AbstractConnectionOperation implements ConnectionOperation {
37
38 44058 jjdelcerro
    final static protected Logger LOGGER = LoggerFactory.getLogger(AbstractConnectionOperation.class);
39 43020 jjdelcerro
40
    final protected JDBCHelper helper;
41 43114 jjdelcerro
42 43377 jjdelcerro
    protected Connection conn = null;
43 43020 jjdelcerro
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 44198 jjdelcerro
            if( this.helper.isThreadSafe() ) {
66
                return this.perform_operation();
67
            }
68
            synchronized(AbstractConnectionOperation.class) {
69
                return this.perform_operation();
70
            }
71 43020 jjdelcerro
        } 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 43377 jjdelcerro
    protected Connection getConnection() throws AccessResourceException {
79
        if( this.conn == null ) {
80
            this.conn = this.helper.getConnection();
81
        }
82 43114 jjdelcerro
        return this.conn;
83
    }
84
85 43377 jjdelcerro
    protected Object perform_operation() throws Exception {
86
        Object result = null;
87 44750 jjdelcerro
        LOGGER.trace("preparing execution of "+this.getClass().getSimpleName()+".");
88 43377 jjdelcerro
        if( needTransaction() ) {
89
            try {
90 44191 jjdelcerro
                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 44750 jjdelcerro
                        LOGGER.trace("Excuting operation {}.", this.getClass().getSimpleName());
100 44191 jjdelcerro
                        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 43377 jjdelcerro
                    }
109 44191 jjdelcerro
                    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 43377 jjdelcerro
                    try {
123 44750 jjdelcerro
                        LOGGER.trace("Excuting operation {}.", this.getClass().getSimpleName());
124 44191 jjdelcerro
                        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 43377 jjdelcerro
                    }
133 44191 jjdelcerro
                    this.getConnection().commit();
134 43377 jjdelcerro
                }
135
            } finally {
136 45437 fdiaz
                closeConnection(result);
137 43020 jjdelcerro
            }
138 43377 jjdelcerro
        } else {
139 43020 jjdelcerro
            try {
140 44750 jjdelcerro
                LOGGER.trace("Excuting operation {}.", this.getClass().getSimpleName());
141 43377 jjdelcerro
                result = perform(this.getConnection());
142 43020 jjdelcerro
            } catch (Exception ex) {
143
                throw new RuntimeException("Can't perform operation '"+this.getClass().getSimpleName()+"'.",ex);
144 43377 jjdelcerro
            } finally {
145 45437 fdiaz
                closeConnection(result);
146 43020 jjdelcerro
            }
147
        }
148 43377 jjdelcerro
        return result;
149 43020 jjdelcerro
    }
150 45437 fdiaz
151
    /**
152
     *
153
     * @param result the result of the operation recently executed
154
     * @throws Exception
155
     */
156
    protected void closeConnection(Object result) throws Exception {
157
        helper.closeConnection(this.getConnection());
158
        conn = null;
159
    }
160 43020 jjdelcerro
}