1 /*
2 * Hibernate, Relational Persistence for Idiomatic Java
3 *
4 * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
5 * indicated by the @author tags or express copyright attribution
6 * statements applied by the authors. All third-party contributions are
7 * distributed under license by Red Hat Middleware LLC.
8 *
9 * This copyrighted material is made available to anyone wishing to use, modify,
10 * copy, or redistribute it subject to the terms and conditions of the GNU
11 * Lesser General Public License, as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this distribution; if not, write to:
20 * Free Software Foundation, Inc.
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02110-1301 USA
23 *
24 */
25 package org.hibernate.jdbc;
26
27 import java.sql.PreparedStatement;
28 import java.sql.SQLException;
29
30 import org.hibernate.HibernateException;
31 import org.hibernate.Interceptor;
32
33 /**
34 * An implementation of the <tt>Batcher</tt> interface that
35 * actually uses batching
36 * @author Gavin King
37 */
38 public class BatchingBatcher extends AbstractBatcher {
39
40 private int batchSize;
41 private Expectation[] expectations;
42
43 public BatchingBatcher(ConnectionManager connectionManager, Interceptor interceptor) {
44 super( connectionManager, interceptor );
45 expectations = new Expectation[ getFactory().getSettings().getJdbcBatchSize() ];
46 }
47
48 public void addToBatch(Expectation expectation) throws SQLException, HibernateException {
49 if ( !expectation.canBeBatched() ) {
50 throw new HibernateException( "attempting to batch an operation which cannot be batched" );
51 }
52 PreparedStatement batchUpdate = getStatement();
53 batchUpdate.addBatch();
54 expectations[ batchSize++ ] = expectation;
55 if ( batchSize == getFactory().getSettings().getJdbcBatchSize() ) {
56 doExecuteBatch( batchUpdate );
57 }
58 }
59
60 protected void doExecuteBatch(PreparedStatement ps) throws SQLException, HibernateException {
61 if ( batchSize == 0 ) {
62 log.debug( "no batched statements to execute" );
63 }
64 else {
65 if ( log.isDebugEnabled() ) {
66 log.debug( "Executing batch size: " + batchSize );
67 }
68
69 try {
70 checkRowCounts( ps.executeBatch(), ps );
71 }
72 catch (RuntimeException re) {
73 log.error( "Exception executing batch: ", re );
74 throw re;
75 }
76 finally {
77 batchSize = 0;
78 }
79
80 }
81
82 }
83
84 private void checkRowCounts(int[] rowCounts, PreparedStatement ps) throws SQLException, HibernateException {
85 int numberOfRowCounts = rowCounts.length;
86 if ( numberOfRowCounts != batchSize ) {
87 log.warn( "JDBC driver did not return the expected number of row counts" );
88 }
89 for ( int i = 0; i < numberOfRowCounts; i++ ) {
90 expectations[i].verifyOutcome( rowCounts[i], ps, i );
91 }
92 }
93
94 }
95
96
97
98
99
100