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

History | View | Annotate | Download (3.07 KB)

1
package org.gvsig.fmap.dal.store.jdbc2.spi.operations;
2

    
3
import java.sql.Connection;
4
import java.sql.SQLException;
5
import java.util.logging.Level;
6
import org.gvsig.fmap.dal.exception.DataException;
7
import org.gvsig.fmap.dal.resource.ResourceAction;
8
import org.gvsig.fmap.dal.resource.spi.ResourceProvider;
9
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCTransactionRollbackException;
10
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
11
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase;
12
import org.slf4j.Logger;
13
import org.slf4j.LoggerFactory;
14

    
15
public abstract class AbstractConnectionOperation implements ConnectionOperation {
16

    
17
    final static protected Logger logger = LoggerFactory.getLogger(AbstractConnectionOperation.class);
18

    
19
    final protected JDBCHelper helper;
20
    
21
    private Connection conn = null;
22

    
23
    public AbstractConnectionOperation(JDBCHelper helper) {
24
        this.helper = helper;
25
    }
26

    
27
    @Override
28
    public boolean continueTransactionAllowed() {
29
        return false;
30
    }
31

    
32
    @Override
33
    public boolean needTransaction() {
34
        return true;
35
    }
36

    
37
    protected JDBCSQLBuilderBase createSQLBuilder() {
38
        return this.helper.createSQLBuilder();
39
    }
40

    
41
    @Override
42
    public Object perform()  {
43
        try {
44
            return this.perform_operation();
45
        } catch (RuntimeException ex) {
46
            throw ex;
47
        } catch (Exception ex) {
48
            throw new RuntimeException("Can't perform operation '"+this.getClass().getSimpleName()+"'.",ex);
49
        }
50
    }
51

    
52
    protected Connection getConnection() {
53
        return this.conn;
54
    }
55
    
56
    public Object perform_operation() throws Exception {
57
            Object result = null;
58
        try {
59
            logger.debug("preparing execution of "+this.getClass().getSimpleName()+".");
60
            conn = helper.getConnection();
61

    
62
            // XXX OJO esta condicion NO ES FIABLE
63
            if (!conn.getAutoCommit()) {
64
                if (!continueTransactionAllowed()) {
65
                    throw new SQLException("nested operations not allowed.");
66
                }
67
            }
68
            conn.setAutoCommit(false);
69
            try {
70
                logger.debug("Excuting operation {}.", this.getClass().getSimpleName());
71
                result = perform(conn);
72
            } catch (Exception ex) {
73
                try {
74
                    conn.rollback();
75
                } catch (Exception e1) {
76
                    throw new JDBCTransactionRollbackException(e1, ex);
77
                }
78
                throw new RuntimeException("Can't perform operation '"+this.getClass().getSimpleName()+"'.",ex);
79
            }
80
            conn.commit();
81
            return result;
82
        } finally {
83
            helper.closeConnection(conn);
84
            conn = null;
85
        }
86
    }
87
//
88
//    public Object perform2() {
89
//        ResourceProvider resource = this.helper.getResource();
90
//        Object r = resource.execute(new ResourceAction() {
91
//
92
//            @Override
93
//            public Object run() throws Exception {
94
//                return perform_operation();
95
//            }
96
//        });
97
//        return r;
98
//    }
99

    
100
}