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
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
public abstract class AbstractConnectionOperation implements ConnectionOperation {
13

    
14
    final static protected Logger LOGGER = LoggerFactory.getLogger(AbstractConnectionOperation.class);
15

    
16
    final protected JDBCHelper helper;
17
    
18
    protected Connection conn = null;
19

    
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
    protected Connection getConnection() throws AccessResourceException {
50
        if( this.conn == null ) {
51
            this.conn = this.helper.getConnection();
52
        }
53
        return this.conn;
54
    }
55
    
56
    protected Object perform_operation() throws Exception {
57
        Object result = null;
58
        LOGGER.debug("preparing execution of "+this.getClass().getSimpleName()+".");
59
        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
                }
67
                this.getConnection().setAutoCommit(false);
68
                try {
69
                    LOGGER.debug("Excuting operation {}.", this.getClass().getSimpleName());
70
                    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
            }
84
        } else {
85
            try {
86
                LOGGER.debug("Excuting operation {}.", this.getClass().getSimpleName());
87
                result = perform(this.getConnection());
88
            } catch (Exception ex) {
89
                throw new RuntimeException("Can't perform operation '"+this.getClass().getSimpleName()+"'.",ex);
90
            } finally {
91
                helper.closeConnection(this.getConnection());
92
                conn = null;
93
            }
94
        }
95
        return result;
96
    }
97
}