Revision 45533 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
AppendOperation.java | ||
---|---|---|
42 | 42 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
43 | 43 |
import org.gvsig.tools.dispose.Disposable; |
44 | 44 |
import org.gvsig.tools.dispose.DisposeUtils; |
45 |
import org.slf4j.LoggerFactory; |
|
45 | 46 |
|
46 | 47 |
|
47 | 48 |
public class AppendOperation { |
49 |
protected static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(AppendOperation.class); |
|
48 | 50 |
|
49 | 51 |
protected Connection connection = null; |
50 | 52 |
protected final JDBCHelper helper; |
... | ... | |
58 | 60 |
protected String insertSQL; |
59 | 61 |
protected int batchCount; |
60 | 62 |
private final int batchSize; |
63 |
private final Disposable[] disposableParameters; |
|
61 | 64 |
|
62 | 65 |
public AppendOperation( |
63 | 66 |
JDBCHelper helper, |
... | ... | |
68 | 71 |
this.table = table; |
69 | 72 |
this.type = type; |
70 | 73 |
this.batchSize = this.helper.getConnectionParameters().getBatchSize(); |
74 |
if( this.batchSize>0 ) { |
|
75 |
this.disposableParameters = new Disposable[this.batchSize]; |
|
76 |
} else { |
|
77 |
this.disposableParameters = null; |
|
78 |
} |
|
71 | 79 |
} |
72 | 80 |
|
73 | 81 |
public void begin() throws DataException { |
... | ... | |
119 | 127 |
} |
120 | 128 |
|
121 | 129 |
protected void clean() { |
130 |
if( this.batchCount > 0 ) { |
|
131 |
this.clearBatch(); |
|
132 |
} |
|
122 | 133 |
JDBCUtils.closeQuietly(this.preparedStatement); |
123 | 134 |
this.helper.closeConnection(this.connection); |
124 | 135 |
this.connection = null; |
... | ... | |
133 | 144 |
return; // In test mode ??? |
134 | 145 |
} |
135 | 146 |
if( this.batchCount > 0 ) { |
136 |
this.batchCount = 0; |
|
137 |
JDBCUtils.executeBatch(this.preparedStatement,this.insertSQL); |
|
147 |
this.executeBatch(); |
|
138 | 148 |
} |
139 |
|
|
140 | 149 |
this.connection.commit(); |
141 | 150 |
for (String sql : this.getPostSQLs()) { |
142 | 151 |
JDBCUtils.execute(this.connection, sql); |
... | ... | |
157 | 166 |
if( this.connection == null ) { |
158 | 167 |
return; // In test mode ??? |
159 | 168 |
} |
169 |
if( this.batchCount > 0 ) { |
|
170 |
this.clearBatch(); |
|
171 |
} |
|
160 | 172 |
this.connection.rollback(); |
161 | 173 |
for (String sql : this.getPostSQLs()) { |
162 | 174 |
JDBCUtils.execute(this.connection, sql); |
... | ... | |
185 | 197 |
|
186 | 198 |
@SuppressWarnings("UseSpecificCatch") |
187 | 199 |
public void append(FeatureProvider feature) throws DataException { |
188 |
int status[] = null; |
|
189 |
Disposable paramsDisposer = null; |
|
190 | 200 |
try { |
191 |
paramsDisposer = this.sqlbuilder.setParameters(this.preparedStatement, feature); |
|
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); |
|
201 |
if( this.batchSize>0 ) { |
|
202 |
this.addBatch(feature); |
|
203 |
if( this.batchCount >= this.batchSize ) { |
|
204 |
this.executeBatch(); |
|
205 |
} |
|
206 |
} else { |
|
207 |
Disposable theParametersDisposable = null; |
|
208 |
try { |
|
209 |
theParametersDisposable = this.sqlbuilder.setParameters(this.preparedStatement, feature); |
|
210 |
int n = JDBCUtils.executeUpdate(this.preparedStatement,this.insertSQL); |
|
211 |
if( n<1 ) { |
|
212 |
throw new RuntimeException("Can't insert feature (n="+n+")."); |
|
213 |
} |
|
214 |
} finally { |
|
215 |
DisposeUtils.disposeQuietly(theParametersDisposable); |
|
216 |
} |
|
196 | 217 |
} |
197 | 218 |
} catch(Exception ex) { |
198 | 219 |
throw new RuntimeException("Can't insert feature.", ex); |
199 |
} finally { |
|
200 |
DisposeUtils.disposeQuietly(paramsDisposer); |
|
201 | 220 |
} |
202 |
if( status!=null ) { |
|
203 |
for (int n : status) { |
|
204 |
if( n<1 ) { |
|
205 |
throw new RuntimeException("Can't insert feature (n="+n+")."); |
|
206 |
} |
|
221 |
} |
|
222 |
|
|
223 |
private void addBatch(FeatureProvider feature) throws SQLException { |
|
224 |
Disposable theParametersDisposable = this.sqlbuilder.setParameters(this.preparedStatement, feature); |
|
225 |
JDBCUtils.addBatch(this.preparedStatement,this.insertSQL); |
|
226 |
this.disposableParameters[this.batchCount++] = theParametersDisposable; |
|
227 |
} |
|
228 |
|
|
229 |
private void executeBatch() throws SQLException { |
|
230 |
int[] status = JDBCUtils.executeBatch(this.preparedStatement,this.insertSQL); |
|
231 |
this.clearBatch(); |
|
232 |
for (int n : status) { |
|
233 |
if( n<1 ) { |
|
234 |
throw new RuntimeException("Can't insert feature (n="+n+")."); |
|
207 | 235 |
} |
208 | 236 |
} |
209 | 237 |
} |
238 |
|
|
239 |
private void clearBatch() { |
|
240 |
try { |
|
241 |
this.preparedStatement.clearParameters(); |
|
242 |
this.preparedStatement.clearBatch(); |
|
243 |
for (int i = 0; i < this.batchCount && i < this.disposableParameters.length; i++) { |
|
244 |
DisposeUtils.dispose(this.disposableParameters[i]); |
|
245 |
this.disposableParameters[i] = null; |
|
246 |
} |
|
247 |
this.batchCount = 0; |
|
248 |
} catch (SQLException ex) { |
|
249 |
LOGGER.warn("Can't clear batch statement", ex); |
|
250 |
} |
|
251 |
} |
|
210 | 252 |
} |
Also available in: Unified diff