From: | Tillmann Schulz <tillmann73(at)yahoo(dot)de> |
---|---|
To: | "pgsql-jdbc(at)postgresql(dot)org" <pgsql-jdbc(at)postgresql(dot)org> |
Subject: | Re: Return Codes of BatchUpdateException in PostgreSql 9.6 |
Date: | 2016-10-19 11:49:20 |
Message-ID: | 65622026.6539291.1476877760626@mail.yahoo.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-jdbc |
Hi,
thanks for the response.
I have a simple test program which first creates a testtable, fills it with one record and does a batch insert of 10 records.
The 5th insert causes a BatchUpdateException at stmt.executeBatch() when the data is NOT commited.
The question is, which return codes should be in x.getUpdateCounts().
For better reading I placed the test programm on http://pastebin.com/0P1S6zEc
Bye,
Tillmann
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class TestUpdateCount
{
public static void main( String[] args ) throws SQLException, ClassNotFoundException, IOException
{
//////////////////////////////////////////////////////////////////////////////////////
// CHANGE THIS
String dbUrl = "jdbc:postgresql://localhost:5432/db?charSet=UTF-8";
String dbUser = "user";
String dbPassword = "xxxx";
//////////////////////////////////////////////////////////////////////////////////////
try
{
Connection con = openConnection( dbUrl, dbUser, dbPassword );
con.setAutoCommit( false );
try
{
// Prepare Table
Statement stmtDrop = con.createStatement();
stmtDrop.execute( "DROP TABLE IF EXISTS TESTTABLE " );
con.commit();
Statement stmtCreate = con.createStatement();
stmtCreate.execute( "CREATE TABLE TESTTABLE (id INTEGER PRIMARY KEY)" );
con.commit();
Statement stmtData = con.createStatement();
stmtData.execute( "INSERT INTO TESTTABLE VALUES (5)" );
con.commit();
// TestCase: Insert 10 statements via jdbc batch.
// 5h statement should fail because of duplicate key error
con = openConnection( dbUrl, dbUser, dbPassword );
con.setAutoCommit( false );
Statement stmt = con.createStatement();
for( int i = 0; i < 10; i++ )
{
stmt.addBatch( "INSERT INTO TESTTABLE VALUES('" + i + "')" );
}
stmt.executeBatch();
con.commit();
}
catch( java.sql.BatchUpdateException x )
{
final int[] updateCounts = x.getUpdateCounts();
for( int i = 0; i < updateCounts.length; i++ )
{
System.err.println( "updateCounts[" + i + "]=" + updateCounts[i] );
}
// First 5 statements are successfully, so update count should be 1
for( int i = 0; i < 5; i++ )
{
if( updateCounts[i] != 1 )
System.err.println( "Wrong information returned by driver for update Count " + i );
}
// 5th statement
if( updateCounts[5] == -3 )
System.err.println( "Correct information returned by driver for update Count " + 5 );
}
}
catch( SQLException s )
{
s.printStackTrace();
if( s.getNextException() != null )
{
s.getNextException().printStackTrace();
}
}
catch( Exception s )
{
s.printStackTrace();
}
}
private static Connection openConnection( String url, String user, String password )
throws ClassNotFoundException, SQLException, IOException
{
try
{
Class.forName( "org.postgresql.Driver" );
}
catch( ClassNotFoundException e )
{
System.err.println( "Could not load driver!" );
throw e;
}
Properties props = new Properties();
// Activate for logging
// props.setProperty( "loglevel", "2" );
// FileWriter fw = new FileWriter( "C:\\temp\\test12345.txt" );
// DriverManager.setLogWriter( new PrintWriter( fw ) );
props.setProperty( "user", user );
props.setProperty( "password", password );
return DriverManager.getConnection( url, props );
}
}
From | Date | Subject | |
---|---|---|---|
Next Message | Robert Haas | 2016-10-19 16:04:27 | Re: Patch: Implement failover on libpq connect level. |
Previous Message | Vladimir Sitnikov | 2016-10-19 10:07:22 | Re: Return Codes of BatchUpdateException in PostgreSql 9.6 |