Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TjY4v-0004Hs-H5 for pgsql-jdbc@arkaria.postgresql.org; Fri, 14 Dec 2012 16:25:37 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1TjY4u-0005PI-EO for pgsql-jdbc@arkaria.postgresql.org; Fri, 14 Dec 2012 16:25:36 +0000 Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TjY4t-0005OZ-BB for pgsql-jdbc@postgresql.org; Fri, 14 Dec 2012 16:25:35 +0000 Received: from blu0-omc4-s32.blu0.hotmail.com ([65.55.111.171]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TjY4r-0006MR-Pz for pgsql-jdbc@postgresql.org; Fri, 14 Dec 2012 16:25:34 +0000 Received: from BLU0-SMTP153 ([65.55.111.135]) by blu0-omc4-s32.blu0.hotmail.com with Microsoft SMTPSVC(6.0.3790.4675); Fri, 14 Dec 2012 08:25:32 -0800 X-EIP: [eH7wtlVzBsN2mk6AfrDzYMJ+k+5oLVr9] X-Originating-Email: [liuhaifeng@live.com] Message-ID: Received: from [192.168.1.6] ([220.248.75.6]) by BLU0-SMTP153.blu0.hotmail.com over TLS secured channel with Microsoft SMTPSVC(6.0.3790.4675); Fri, 14 Dec 2012 08:25:32 -0800 Content-Type: text/plain; charset="iso-8859-1" MIME-Version: 1.0 (Mac OS X Mail 6.2 \(1499\)) Subject: Re: Fwd: [ADMIN] Confuse about the behaveior of PreparedStatement.executeBatch (jdbc) From: Haifeng Liu In-Reply-To: <50CB4C40.7000104@ttc-cmc.net> Date: Sat, 15 Dec 2012 00:25:23 +0800 CC: pgsql-jdbc@postgresql.org Content-Transfer-Encoding: quoted-printable References: <50CB4C40.7000104@ttc-cmc.net> To: dmp X-Mailer: Apple Mail (2.1499) X-OriginalArrivalTime: 14 Dec 2012 16:25:32.0405 (UTC) FILETIME=[A3657250:01CDDA17] X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-jdbc Precedence: bulk Sender: pgsql-jdbc-owner@postgresql.org example code: Connection conn =3D getConnection(); PreparedStatement pstmt =3D conn.prepareStatement("insert/update..."); for (A a : AList) { pstmt.setParameter(...); pstmt.addBatch(); } pstmt.executeBatch(); I did a simple test and found that if one of the batch failed, the other up= date may be execute partially. when I test with 10 updates a batch, none of= them are updated, when I test with 1000 updates a batch, about 700+ of the= m are executed, but the failed update should be the last one, which means 9= 99 executed updates is more reasonable than 700+. But really weird thing is= the getUpdateCounts method returns the reasonable information. I don't know what happened inside this driver. Currently I have to setAutoC= ommit to false and use commit/rollback to achieve my goal. On Dec 14, 2012, at 11:56 PM, dmp wrote: > Hello, >=20 > Though a simple example of your code would provide a better response > from the mailing list, I will speculate based on the context of the > Java 6 API, statement class and your comments. Yes, the batch appears > to be proceeding. >=20 > Statement.executeBatch(). >=20 > executeBatch >=20 > int[] executeBatch() throws SQLException > ~ > ~ > ~ > If one of the commands in a batch update fails to execute proper= ly, this method throws a BatchUpdateException, and a JDBC driver may or may= not continue to process the remaining commands in the batch. However, the = driver's behavior must be consistent with a particular DBMS, either always = continuing to process commands or never continuing to process commands. If = the driver continues processing after a failure, the array returned by the = method BatchUpdateException.getUpdateCounts will contain as many elements a= s there are commands in the batch, and at least one of the elements will be= the following: > ~ > ~ > ~ > Returns: > an array of update counts containing one element for each command = in the batch. The elements of the array are ordered according to the order = in which commands were added to the batch. >=20 > Throws: > SQLException - if a database access error occurs, this method is calle= d on a closed Statement or the driver does not support batch statements. Th= rows BatchUpdateException (a subclass of SQLException) if one of the comman= ds sent to the database fails to execute properly or attempts to return a r= esult set. >=20 > You may be able to answer your own question by examining the int[] array = that > is returned by way of BatchUpdateException.getUpdateCounts() >=20 > danap. >=20 > Haifeng Liu wrote: >>> Hi, >>>=20 >>> I have a DB writer program written in Java, it do batch updates, and >>> will try to update each record in a batch if it failed. I think no any >>> record in a batch will be update if the batch failed, but weirdly I >>> found a few records update twice(very little amount among all records). >>>=20 >>> Well, there is less stuff about postgresql jdbc driver. I wanna know >>> if executeBatch really keep all the records in a batch untouched when >>> the batch failed. Any help is appreciate. >=20 >=20 --=20 Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-jdbc