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

History | View | Annotate | Download (6.5 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.jdbc.exception.SQLRuntimeException;
31
import org.gvsig.fmap.dal.store.jdbc2.JDBCConnection;
32
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
33
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase;
34
import org.slf4j.Logger;
35
import org.slf4j.LoggerFactory;
36

    
37
@SuppressWarnings("UseSpecificCatch")
38
public abstract class AbstractConnectionOperation implements ConnectionOperation {
39

    
40
    final static protected Logger LOGGER = LoggerFactory.getLogger(AbstractConnectionOperation.class);
41

    
42
    final protected JDBCHelper helper;
43
    
44
    protected JDBCConnection conn = null;
45

    
46
    public AbstractConnectionOperation(JDBCHelper helper) {
47
        this.helper = helper;
48
    }
49

    
50
    @Override
51
    public boolean continueTransactionAllowed() {
52
        return this.helper.getTransaction()!=null;
53
    }
54

    
55
    @Override
56
    public boolean needTransaction() {
57
        return true;
58
    }
59

    
60
    protected JDBCSQLBuilderBase createSQLBuilder() {
61
        return this.helper.createSQLBuilder();
62
    }
63

    
64
    @Override
65
    public Object perform()  {
66
        try {
67
            if( this.helper.isThreadSafe() ) {
68
                return this.perform_operation();
69
            } 
70
            synchronized(AbstractConnectionOperation.class) {
71
                return this.perform_operation();
72
            }
73
        } catch (RuntimeException ex) {
74
            throw ex;
75
        } catch (Exception ex) {
76
            throw new RuntimeException("Can't perform operation '"+this.getClass().getSimpleName()+"'.",ex);
77
        }
78
    }
79

    
80
    protected JDBCConnection getConnection() throws AccessResourceException {
81
        if( this.conn == null ) {
82
            this.conn = this.helper.getConnection();
83
        }
84
        return this.conn;
85
    }
86
    
87
    protected Object perform_operation() throws Exception {
88
        Object result = null;
89
        LOGGER.trace("preparing execution of "+this.getClass().getSimpleName()+".");
90
        if( needTransaction() ) {
91
            try {
92
                if( this.helper.allowNestedOperations() ) {
93
                    boolean commitOnFinish = true;
94
                    if (!this.getConnection().isInTransaction()) {
95
                        commitOnFinish = false;
96
                    } 
97
                    this.getConnection().begin();
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
                        
112
                    }
113
                } else {
114
                    if (this.getConnection().isInTransaction()) {
115
                        if (!continueTransactionAllowed()) {
116
                            throw new SQLException("nested operations not allowed.");
117
                        }
118
                    }
119
                    this.getConnection().begin();
120
                    try {
121
                        LOGGER.trace("Excuting operation {}.", this.getClass().getSimpleName());
122
                        result = perform(this.getConnection());
123
                    } catch (SQLRuntimeException ex) {
124
                        try {
125
                            this.getConnection().rollback();
126
                        } catch (Exception e1) {
127
                            throw new JDBCTransactionRollbackException(e1, ex);
128
                        }
129
                        throw new SQLRuntimeException("Can't perform operation '"+this.getClass().getSimpleName()+"'.",ex);
130
                    } catch (Exception ex) {
131
                        try {
132
                            this.getConnection().rollback();
133
                        } catch (Exception e1) {
134
                            throw new JDBCTransactionRollbackException(e1, ex);
135
                        }
136
                        throw new RuntimeException("Can't perform operation '"+this.getClass().getSimpleName()+"'.",ex);
137
                    }
138
                    this.getConnection().commit();
139
                }
140
            } finally {
141
                closeConnection(result);
142
            }
143
        } else {
144
            try {
145
                LOGGER.trace("Excuting operation {}.", this.getClass().getSimpleName());
146
                result = perform(this.getConnection());
147
            } catch (SQLRuntimeException ex) {
148
                throw new SQLRuntimeException("Can't perform operation '"+this.getClass().getSimpleName()+"'.",ex);
149
            } catch (Exception ex) {
150
                throw new RuntimeException("Can't perform operation '"+this.getClass().getSimpleName()+"'.",ex);
151
            } finally {
152
                closeConnection(result);
153
            }
154
        }
155
        return result;
156
    }
157
    
158
    /**
159
     * 
160
     * @param result the result of the operation recently executed
161
     * @throws Exception
162
     */
163
    protected void closeConnection(Object result) throws Exception {
164
        this.getConnection().close();
165
        conn = null;
166
    }
167
}