Revision 43377 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/AppendOperation.java

View differences:

AppendOperation.java
4 4
import java.sql.Connection;
5 5
import java.sql.PreparedStatement;
6 6
import java.sql.SQLException;
7
import java.util.logging.Level;
8
import java.util.logging.Logger;
7 9
import org.gvsig.fmap.dal.DataTypes;
8 10
import org.gvsig.fmap.dal.exception.DataException;
9 11
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
......
51 53
        }
52 54

  
53 55
        try {
54
            this.connection = this.helper.getConnection();
56
            this.connection = this.helper.getConnectionWritable();
55 57
            
56 58
            this.sqlbuilder = this.helper.createSQLBuilder();
57 59

  
......
76 78
            PreparedStatement st;
77 79
            this.sql = this.sqlbuilder.insert().toString();
78 80
            this.preparedStatement = this.connection.prepareStatement(sql);
81
            this.connection.setAutoCommit(false);
79 82
            
80 83
        } catch (SQLException ex) {
81 84
            throw new JDBCPreparingSQLException(this.sqlbuilder.toString(),ex);
......
83 86

  
84 87
    }
85 88
    
86
    public void end() {
87
        JDBCUtils.closeQuietly(this.connection);
89
    protected void clean() {
88 90
        JDBCUtils.closeQuietly(this.preparedStatement);
91
        this.helper.closeConnection(this.connection);
89 92
        this.connection = null;
90 93
        this.preparedStatement = null;
91 94
        this.sqlbuilder = null;
92
        this.sql = null;
95
        this.sql = null;        
93 96
    }
94 97
    
95
    public void abort() {
96
        this.end();
98
    public void end() {
99
        try {
100
            this.connection.commit();
101
        } catch (SQLException ex) {
102
            try {
103
                this.connection.rollback();
104
            } catch (SQLException ex1) {
105
            }
106
            throw new RuntimeException("Can't commit transaction", ex);
107
        } finally {
108
            clean();
109
        }
97 110
    }
98 111
    
112
    public void abort() {        
113
        try {
114
            this.connection.rollback();
115
        } catch (SQLException ex) {
116
        }
117
        clean();
118
    }
119
    
99 120
    public void append(FeatureProvider feature) throws DataException {
121
        int n;
100 122
        try {
101
            this.connection.setAutoCommit(false);
102
            try {
103
                this.sqlbuilder.setParameters(this.preparedStatement, feature);
104
                int n = JDBCUtils.executeUpdate(this.preparedStatement,this.sql);
105
                if ( n > 0) {
106
                    this.connection.commit();
107
                    return;
108
                }              
109
                this.connection.rollback();
110
            } catch(Exception ex) {
111
                try {
112
                    this.connection.rollback();
113
                } catch (Exception e1) {
114
                    throw new JDBCTransactionRollbackException(e1, ex);
115
                }                
116
                throw new JDBCExecuteSQLException(this.sql,ex);
117
            } 
118
            throw new JDBCExecuteSQLException(this.sql,null);
119

  
120
        } catch (JDBCExecuteSQLException ex) {
121
            throw ex;
122
        } catch (Exception ex) {
123
            throw new JDBCExecuteSQLException(this.sql,ex);
123
            this.sqlbuilder.setParameters(this.preparedStatement, feature);
124
            n = JDBCUtils.executeUpdate(this.preparedStatement,this.sql);
125
        } catch(Exception ex) {
126
            throw new RuntimeException("Can't insert feature.", ex);
124 127
        }
128
        if( n<1 ) {
129
            throw new RuntimeException("Can't insert feature (n="+n+").");
130
        }
125 131
    }
126 132
}

Also available in: Unified diff