From: Haifeng Liu <liuhaifeng@live.com>
To: dmp <danap@ttc-cmc.net>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: Fwd: [ADMIN] Confuse about the behaveior of PreparedStatement.executeBatch (jdbc)
Date: Sat, 15 Dec 2012 00:25:23 +0800
Message-ID: <BLU0-SMTP1534C0E74C7BF8FD348BE51B94D0@phx.gbl> (raw)
In-Reply-To: <50CB4C40.7000104@ttc-cmc.net>
References: <BLU0-SMTP1143B3C57133A122263A47AB94E0@phx.gbl>
<BLU0-SMTP23378AE609963CCB543ED38B94D0@phx.gbl>
<50CB4C40.7000104@ttc-cmc.net>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-jdbc>
example code:
Connection conn = getConnection();
PreparedStatement pstmt = 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 update 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 them are executed, but the failed update should be the last one, which means 999 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 setAutoCommit to false and use commit/rollback to achieve my goal.
On Dec 14, 2012, at 11:56 PM, dmp <danap@ttc-cmc.net> wrote:
> Hello,
>
> 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.
>
> Statement.executeBatch().
>
> executeBatch
>
> int[] executeBatch() throws SQLException
> ~
> ~
> ~
> If one of the commands in a batch update fails to execute properly, 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 as 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.
>
> Throws:
> SQLException - if a database access error occurs, this method is called on a closed Statement or the driver does not support batch statements. Throws BatchUpdateException (a subclass of SQLException) if one of the commands sent to the database fails to execute properly or attempts to return a result set.
>
> You may be able to answer your own question by examining the int[] array that
> is returned by way of BatchUpdateException.getUpdateCounts()
>
> danap.
>
> Haifeng Liu wrote:
>>> Hi,
>>>
>>> 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).
>>>
>>> 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.
>
>
--
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-admin@postgresql.org
Cc: liuhaifeng@live.com, danap@ttc-cmc.net, pgsql-jdbc@postgresql.org
Subject: Re: Fwd: [ADMIN] Confuse about the behaveior of PreparedStatement.executeBatch (jdbc)
In-Reply-To: <BLU0-SMTP1534C0E74C7BF8FD348BE51B94D0@phx.gbl>
References: <BLU0-SMTP1143B3C57133A122263A47AB94E0@phx.gbl> <BLU0-SMTP23378AE609963CCB543ED38B94D0@phx.gbl> <50CB4C40.7000104@ttc-cmc.net> <BLU0-SMTP1534C0E74C7BF8FD348BE51B94D0@phx.gbl>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox