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

History | View | Annotate | Download (3.37 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
public abstract class AbstractConnectionOperation implements ConnectionOperation {
13
14 44058 jjdelcerro
    final static protected Logger LOGGER = LoggerFactory.getLogger(AbstractConnectionOperation.class);
15 43020 jjdelcerro
16
    final protected JDBCHelper helper;
17 43114 jjdelcerro
18 43377 jjdelcerro
    protected Connection conn = null;
19 43020 jjdelcerro
20
    public AbstractConnectionOperation(JDBCHelper helper) {
21
        this.helper = helper;
22
    }
23
24
    @Override
25
    public boolean continueTransactionAllowed() {
26
        return false;
27
    }
28
29
    @Override
30
    public boolean needTransaction() {
31
        return true;
32
    }
33
34
    protected JDBCSQLBuilderBase createSQLBuilder() {
35
        return this.helper.createSQLBuilder();
36
    }
37
38
    @Override
39
    public Object perform()  {
40
        try {
41
            return this.perform_operation();
42
        } catch (RuntimeException ex) {
43
            throw ex;
44
        } catch (Exception ex) {
45
            throw new RuntimeException("Can't perform operation '"+this.getClass().getSimpleName()+"'.",ex);
46
        }
47
    }
48
49 43377 jjdelcerro
    protected Connection getConnection() throws AccessResourceException {
50
        if( this.conn == null ) {
51
            this.conn = this.helper.getConnection();
52
        }
53 43114 jjdelcerro
        return this.conn;
54
    }
55
56 43377 jjdelcerro
    protected Object perform_operation() throws Exception {
57
        Object result = null;
58 44058 jjdelcerro
        LOGGER.debug("preparing execution of "+this.getClass().getSimpleName()+".");
59 43377 jjdelcerro
        if( needTransaction() ) {
60
            try {
61
                // XXX OJO esta condicion NO ES FIABLE
62
                if (!this.getConnection().getAutoCommit()) {
63
                    if (!continueTransactionAllowed()) {
64
                        throw new SQLException("nested operations not allowed.");
65
                    }
66 43020 jjdelcerro
                }
67 43377 jjdelcerro
                this.getConnection().setAutoCommit(false);
68
                try {
69 44058 jjdelcerro
                    LOGGER.debug("Excuting operation {}.", this.getClass().getSimpleName());
70 43377 jjdelcerro
                    result = perform(this.getConnection());
71
                } catch (Exception ex) {
72
                    try {
73
                        this.getConnection().rollback();
74
                    } catch (Exception e1) {
75
                        throw new JDBCTransactionRollbackException(e1, ex);
76
                    }
77
                    throw new RuntimeException("Can't perform operation '"+this.getClass().getSimpleName()+"'.",ex);
78
                }
79
                this.getConnection().commit();
80
            } finally {
81
                helper.closeConnection(this.getConnection());
82
                conn = null;
83 43020 jjdelcerro
            }
84 43377 jjdelcerro
        } else {
85 43020 jjdelcerro
            try {
86 44058 jjdelcerro
                LOGGER.debug("Excuting operation {}.", this.getClass().getSimpleName());
87 43377 jjdelcerro
                result = perform(this.getConnection());
88 43020 jjdelcerro
            } catch (Exception ex) {
89
                throw new RuntimeException("Can't perform operation '"+this.getClass().getSimpleName()+"'.",ex);
90 43377 jjdelcerro
            } finally {
91
                helper.closeConnection(this.getConnection());
92
                conn = null;
93 43020 jjdelcerro
            }
94
        }
95 43377 jjdelcerro
        return result;
96 43020 jjdelcerro
    }
97
}