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 43020 jjdelcerro
package org.gvsig.fmap.dal.store.jdbc2.spi.operations;
2
3
import java.sql.Connection;
4
import java.sql.SQLException;
5 43377 jjdelcerro
import org.gvsig.fmap.dal.resource.exception.AccessResourceException;
6 43020 jjdelcerro
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 44191 jjdelcerro
@SuppressWarnings("UseSpecificCatch")
13 43020 jjdelcerro
public abstract class AbstractConnectionOperation implements ConnectionOperation {
14
15 44058 jjdelcerro
    final static protected Logger LOGGER = LoggerFactory.getLogger(AbstractConnectionOperation.class);
16 43020 jjdelcerro
17
    final protected JDBCHelper helper;
18 43114 jjdelcerro
19 43377 jjdelcerro
    protected Connection conn = null;
20 43020 jjdelcerro
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 43377 jjdelcerro
    protected Connection getConnection() throws AccessResourceException {
51
        if( this.conn == null ) {
52
            this.conn = this.helper.getConnection();
53
        }
54 43114 jjdelcerro
        return this.conn;
55
    }
56
57 43377 jjdelcerro
    protected Object perform_operation() throws Exception {
58
        Object result = null;
59 44058 jjdelcerro
        LOGGER.debug("preparing execution of "+this.getClass().getSimpleName()+".");
60 43377 jjdelcerro
        if( needTransaction() ) {
61
            try {
62 44191 jjdelcerro
                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 43377 jjdelcerro
                    }
81 44191 jjdelcerro
                    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 43377 jjdelcerro
                    try {
95 44191 jjdelcerro
                        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 43377 jjdelcerro
                    }
105 44191 jjdelcerro
                    this.getConnection().commit();
106 43377 jjdelcerro
                }
107
            } finally {
108
                helper.closeConnection(this.getConnection());
109
                conn = null;
110 43020 jjdelcerro
            }
111 43377 jjdelcerro
        } else {
112 43020 jjdelcerro
            try {
113 44058 jjdelcerro
                LOGGER.debug("Excuting operation {}.", this.getClass().getSimpleName());
114 43377 jjdelcerro
                result = perform(this.getConnection());
115 43020 jjdelcerro
            } catch (Exception ex) {
116
                throw new RuntimeException("Can't perform operation '"+this.getClass().getSimpleName()+"'.",ex);
117 43377 jjdelcerro
            } finally {
118
                helper.closeConnection(this.getConnection());
119
                conn = null;
120 43020 jjdelcerro
            }
121
        }
122 43377 jjdelcerro
        return result;
123 43020 jjdelcerro
    }
124
}