Revision 45507

View differences:

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/resources/org/gvsig/fmap/dal/store/jdbc/JDBCParameters.xml
45 45
        <field name="Schema" type="string" mandatory="false" group="Basic">
46 46
          <description>Database schema</description>
47 47
        </field>
48
        <field label="Batch size" name="batchSize" type="Integer" mandatory="false" defaultValue="500" group="Advanced">
49
          <description>Batch size used for bulk inserts</description>
50
        </field>
48 51
      </fields>
49 52
    </class>
50 53

  
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/resources/org/gvsig/fmap/dal/store/jdbc2/JDBC2Parameters.xml
45 45
        <field name="Schema" type="string" mandatory="false" group="Basic">
46 46
          <description>Database schema</description>
47 47
        </field>
48
        <field label="Batch size" name="batchSize" type="Integer" mandatory="false" defaultValue="500" group="Advanced">
49
          <description>Batch size used for bulk inserts</description>
50
        </field>
48 51
      </fields>
49 52
    </class>
50 53

  
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/jdbc/JDBCStoreParameters.java
68 68
		return (Integer) this.getDynValue(PORT_PARAMTER_NAME);
69 69
	}
70 70

  
71
        @Override
72
        public int getBatchSize() {
73
            try {
74
                return (int) this.getDynValue(BATCH_SIZE_PARAMETER_NAME);
75
            } catch(Exception ex) {
76
                return DEFAULT_BATCH_SIZE;
77
            }
78
        }
79
        
71 80
	public String getDBName() {
72 81
		return (String) this.getDynValue(DBNAME_PARAMTER_NAME);
73 82
	}
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/jdbc/JDBCConnectionParameters.java
53 53
	 */
54 54
	public static final String URL_PARAMTER_NAME = "url";
55 55

  
56
	public static final String BATCH_SIZE_PARAMETER_NAME = "batchSize";
57
        
58
        public static final int DEFAULT_BATCH_SIZE = 500;
59
        
56 60
	/**
57 61
	 * Return <code>JDBC driver class name</code> parameter
58 62
	 *
......
80 84
	 * @return
81 85
	 */
82 86
	public String getUrl();
87
        
88
        public int getBatchSize();
89
        
83 90

  
84 91
}
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/jdbc/JDBCNewStoreParameters.java
85 85
        return (String) this.getDynValue(URL_PARAMTER_NAME);
86 86
    }
87 87

  
88
    @Override
89
    public int getBatchSize() {
90
        try {
91
            return (int) this.getDynValue(BATCH_SIZE_PARAMETER_NAME);
92
        } catch(Exception ex) {
93
            return DEFAULT_BATCH_SIZE;
94
        }
95
    }
96

  
88 97
    /**
89 98
     * Set <code>JDBC connection url</code> parameter
90 99
     *
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/jdbc/JDBCResourceParameters.java
92 92
		this.setDynValue(URL_PARAMTER_NAME, url);
93 93
	}
94 94

  
95
        @Override
96
        public int getBatchSize() {
97
            try {
98
                return (int) this.getDynValue(BATCH_SIZE_PARAMETER_NAME);
99
            } catch(Exception ex) {
100
                return DEFAULT_BATCH_SIZE;
101
            }
102
        }
103

  
95 104
}
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/jdbc/JDBCServerExplorerParameters.java
84 84
		this.setDynValue(URL_PARAMTER_NAME, url);
85 85
	}
86 86

  
87
        @Override
88
        public int getBatchSize() {
89
            try {
90
                return (int) this.getDynValue(BATCH_SIZE_PARAMETER_NAME);
91
            } catch(Exception ex) {
92
                return DEFAULT_BATCH_SIZE;
93
            }
94
        }
95

  
87 96
}
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
45 45

  
46 46

  
47 47
public class AppendOperation {
48

  
48
    
49 49
    protected Connection connection = null;
50 50
    protected final JDBCHelper helper;
51 51
    protected final TableReference table;
......
56 56

  
57 57
    protected PreparedStatement preparedStatement;
58 58
    protected String insertSQL;
59
    protected int batchCount;
60
    private final int batchSize;
59 61
    
60 62
    public AppendOperation(
61 63
            JDBCHelper helper, 
......
65 67
        this.helper = helper;
66 68
        this.table = table;
67 69
        this.type = type;
70
        this.batchSize = this.helper.getConnectionParameters().getBatchSize();
68 71
    }
69 72
    
70 73
    public void begin() throws DataException {
......
129 132
            if( this.connection == null ) {
130 133
              return; // In test mode ???
131 134
            }
135
            if( this.batchCount > 0 ) {
136
                this.batchCount = 0;
137
                JDBCUtils.executeBatch(this.preparedStatement,this.insertSQL);
138
            }
139

  
132 140
            this.connection.commit();
133 141
            for (String sql : this.getPostSQLs()) {
134 142
              JDBCUtils.execute(this.connection, sql);
......
175 183
      return this.sqlbuilder.getParameters(feature);
176 184
    }
177 185
    
186
    @SuppressWarnings("UseSpecificCatch")
178 187
    public void append(FeatureProvider feature) throws DataException {
179
        int n;
188
        int status[] = null;
180 189
        Disposable paramsDisposer = null;
181 190
        try {
182 191
            paramsDisposer = this.sqlbuilder.setParameters(this.preparedStatement, feature);
183
            n = JDBCUtils.executeUpdate(this.preparedStatement,this.insertSQL);
192
            JDBCUtils.addBatch(this.preparedStatement,this.insertSQL);
193
            if( ++this.batchCount >= this.batchSize ) {
194
                this.batchCount = 0;
195
                status = JDBCUtils.executeBatch(this.preparedStatement,this.insertSQL);
196
            }
184 197
        } catch(Exception ex) {
185 198
            throw new RuntimeException("Can't insert feature.", ex);
186 199
        } finally {
187 200
            DisposeUtils.disposeQuietly(paramsDisposer);
188 201
        }
189
        if( n<1 ) {
190
            throw new RuntimeException("Can't insert feature (n="+n+").");
202
        if( status!=null ) {
203
            for (int n : status) {
204
                if( n<1 ) {
205
                    throw new RuntimeException("Can't insert feature (n="+n+").");
206
                }
207
            }
191 208
        }
192 209
    }
193 210
}

Also available in: Unified diff