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

History | View | Annotate | Download (4.81 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
            return this.perform_operation();
43
        } catch (RuntimeException ex) {
44
            throw ex;
45
        } catch (Exception ex) {
46
            throw new RuntimeException("Can't perform operation '"+this.getClass().getSimpleName()+"'.",ex);
47
        }
48
    }
49

    
50
    protected Connection getConnection() throws AccessResourceException {
51
        if( this.conn == null ) {
52
            this.conn = this.helper.getConnection();
53
        }
54
        return this.conn;
55
    }
56
    
57
    protected Object perform_operation() throws Exception {
58
        Object result = null;
59
        LOGGER.debug("preparing execution of "+this.getClass().getSimpleName()+".");
60
        if( needTransaction() ) {
61
            try {
62
                if( this.helper.allowNestedOperations() ) {
63
                    boolean commitOnFinish = true;
64
                    if (!this.getConnection().getAutoCommit()) {
65
                        // Casi seguro que es una operacion anidada, asi que no
66
                        // terminamos la transaccion.
67
                        commitOnFinish = false;
68
                    } 
69
                    this.getConnection().setAutoCommit(false);
70
                    try {
71
                        LOGGER.debug("Excuting operation {}.", this.getClass().getSimpleName());
72
                        result = perform(this.getConnection());
73
                    } catch (Exception ex) {
74
                        try {
75
                            this.getConnection().rollback();
76
                        } catch (Exception e1) {
77
                            throw new JDBCTransactionRollbackException(e1, ex);
78
                        }
79
                        throw new RuntimeException("Can't perform operation '"+this.getClass().getSimpleName()+"'.",ex);
80
                    }
81
                    if( commitOnFinish ) {
82
                        this.getConnection().commit();
83
                        this.getConnection().setAutoCommit(true);
84
                    }
85
                } else {
86
                    if (!this.getConnection().getAutoCommit()) {
87
                        // Casi seguro que es una transaccion anidada, si no
88
                        // esta permitido, petamos.
89
                        if (!continueTransactionAllowed()) {
90
                            throw new SQLException("nested operations not allowed.");
91
                        }
92
                    }
93
                    this.getConnection().setAutoCommit(false);
94
                    try {
95
                        LOGGER.debug("Excuting operation {}.", this.getClass().getSimpleName());
96
                        result = perform(this.getConnection());
97
                    } catch (Exception ex) {
98
                        try {
99
                            this.getConnection().rollback();
100
                        } catch (Exception e1) {
101
                            throw new JDBCTransactionRollbackException(e1, ex);
102
                        }
103
                        throw new RuntimeException("Can't perform operation '"+this.getClass().getSimpleName()+"'.",ex);
104
                    }
105
                    this.getConnection().commit();
106
                }
107
            } finally {
108
                helper.closeConnection(this.getConnection());
109
                conn = null;
110
            }
111
        } else {
112
            try {
113
                LOGGER.debug("Excuting operation {}.", this.getClass().getSimpleName());
114
                result = perform(this.getConnection());
115
            } catch (Exception ex) {
116
                throw new RuntimeException("Can't perform operation '"+this.getClass().getSimpleName()+"'.",ex);
117
            } finally {
118
                helper.closeConnection(this.getConnection());
119
                conn = null;
120
            }
121
        }
122
        return result;
123
    }
124
}