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

History | View | Annotate | Download (2.96 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
    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
    public Object perform_operation() throws Exception {
51
            Object result = null;
52
        Connection conn = null;
53
        try {
54
            logger.debug("preparing execution of "+this.getClass().getSimpleName()+".");
55
            conn = helper.getConnection();
56

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

    
94
}