Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 75753B5DC26 for ; Tue, 30 Aug 2011 10:44:45 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 77549-10 for ; Tue, 30 Aug 2011 13:44:38 +0000 (UTC) X-Greylist: delayed 00:08:46.263304 by SQLgrey-1.7.6 Received: from snelders.xs4all.nl (095-097-091-055.static.chello.nl [95.97.91.55]) by mail.postgresql.org (Postfix) with ESMTP id 9CC15B5DC1E for ; Tue, 30 Aug 2011 10:44:37 -0300 (ADT) Received: by snelders.xs4all.nl (Postfix, from userid 2) id 63500517AF1; Tue, 30 Aug 2011 15:18:56 +0200 (CEST) To: Subject: Postgresql ACID =?UTF-8?Q?bug=3F?= MIME-Version: 1.0 Date: Tue, 30 Aug 2011 15:18:56 +0200 From: Jan Snelders Message-ID: X-Sender: jan@snelders.net User-Agent: RoundCube Webmail/0.2.2 Content-Type: multipart/alternative; boundary="=_b0a9ad2952644d458cabb92c0541e796" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.555 tagged_above=-5 required=5 tests=BAYES_00=-1.9, FILL_THIS_FORM_FRAUD_PHISH=0.334, HTML_MESSAGE=0.001, T_FILL_THIS_FORM_SHORT=0.01 X-Spam-Level: X-Archive-Number: 201108/186 X-Sequence-Number: 30998 --=_b0a9ad2952644d458cabb92c0541e796 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Introduction I wrote small java program which performs some selects in one transaction (and thread) and one delete in another transaction and thread on the same table holding one record initially. ENVIRONMENT OS: Centos 5.5 kernel 2.6.18-194.32.1.el5 Postgresql: 9.0.4 jdbc driver: 9.0-801 JDBC 4 RESULTS AND EXPECTATIONS: The program starts with a select (SELECT transaction) and prints one record ('id' and 'address' from RelationAddresses table) The program now deletes the record in another transaction (DELETE TRANSACTION) The program now selects and prints the records from the SELECT transaction. (result as expected one record printed) The DELETE transaction commits. The program now selects and prints the records from the SELECT transaction. (0 records printed, we expected one record since we are still within the SELECT transaction which started while this record was still available) [code] import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class TransactionTest { /**/ private static final String driver = "org.postgresql.Driver"; private static final String connection = "jdbc:postgresql://192.168.0.1/sampledb"; private static final String username = "postgres"; private static final String password = "secret"; private static final String escapeChar = """; /**/ /* private static final String driver = "com.mysql.jdbc.Driver"; private static final String connection = "jdbc:mysql://192.168.0.1/sampledb"; private static final String username = "root"; private static final String password = "secret"; private static final String escapeChar = "`"; /**/ private TransactionTest(){ Select select = new Select(); select.start(); try { Thread.sleep(1000); } catch (InterruptedException e) { /*IGNORE*/ } Delete delete = new Delete(); delete.start(); } public static void main(String[] args) { new TransactionTest(); } Object block = new Object(); class Select extends Thread{ @Override public void run(){ try { Class.forName(driver).newInstance(); Connection conn = DriverManager.getConnection(connection, username, password); conn.setAutoCommit(false); System.out.println("------------"); System.out.println("SELECT TRANSACTION: List all addresses:"); String query = "SELECT " + escapeChar + "relationAddressesId" + escapeChar + ", " + escapeChar + "address" + escapeChar + " FROM " + escapeChar + "RelationAddresses" + escapeChar + ""; Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(query); while(rs.next()){ System.out.println("relationAddressesId: " + rs.getInt(1) + " - address: " + rs.getString(2)); } rs.close(); stmt.close(); System.out.println("------------"); synchronized(block){ try { block.wait(); } catch (InterruptedException e) { /*IGNORE*/} } System.out.println("------------"); System.out.println("SELECT TRANSACTION: List all addresses before DELETE TRANSACTION commit():"); query = "SELECT " + escapeChar + "relationAddressesId" + escapeChar + ", " + escapeChar + "address" + escapeChar + " FROM " + escapeChar + "RelationAddresses" + escapeChar + ""; stmt = conn.createStatement(); rs = stmt.executeQuery(query); while(rs.next()){ System.out.println("relationAddressesId: " + rs.getInt(1) + " - address: " + rs.getString(2)); } rs.close(); stmt.close(); System.out.println("------------"); synchronized(block){ try { block.wait(); } catch (InterruptedException e) { /*IGNORE*/} } System.out.println("------------"); System.out.println("SELECT TRANSACTION: List all addresses after DELETE TRANSACTION commit():"); query = "SELECT " + escapeChar + "relationAddressesId" + escapeChar + ", " + escapeChar + "address" + escapeChar + " FROM " + escapeChar + "RelationAddresses" + escapeChar + ""; stmt = conn.createStatement(); rs = stmt.executeQuery(query); while(rs.next()){ System.out.println("relationAddressesId: " + rs.getInt(1) + " - address: " + rs.getString(2)); } rs.close(); stmt.close(); System.out.println("------------"); conn.commit(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } } class Delete extends Thread{ @Override public void run(){ try { Class.forName(driver).newInstance(); Connection conn = DriverManager.getConnection(connection, username, password); conn.setAutoCommit(false); System.out.println(""); System.out.println("DELETE TRANSACTION: Deleting addresses...."); System.out.println(""); String query = "DELETE FROM " + escapeChar + "RelationAddresses" + escapeChar + ""; Statement stmt = conn.createStatement(); stmt.executeUpdate(query); stmt.close(); synchronized(block){ block.notifyAll(); } try { Thread.sleep(1000); } catch (InterruptedException e) {/*IGNORE*/} conn.commit(); synchronized(block){ block.notifyAll(); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } } } [/code] EXPECTED OUTPUT: ------------ SELECT TRANSACTION: List all addresses: relationAddressesId: 1 - address: Some address ------------ DELETE TRANSACTION: Deleting addresses.... ------------ SELECT TRANSACTION: List all addresses before DELETE TRANSACTION commit(): relationAddressesId: 1 - address: Some address ------------ ------------ SELECT TRANSACTION: List all addresses after DELETE TRANSACTION commit(): relationAddressesId: 1 - address: Some address ------------ ACTUAL OUTPUT: ------------ SELECT TRANSACTION: List all addresses: relationAddressesId: 1 - address: Some address ------------ DELETE TRANSACTION: Deleting addresses.... ------------ SELECT TRANSACTION: List all addresses before DELETE TRANSACTION commit(): relationAddressesId: 1 - address: Some address ------------ ------------ SELECT TRANSACTION: List all addresses after DELETE TRANSACTION commit(): ------------ --=_b0a9ad2952644d458cabb92c0541e796 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=UTF-8

Introduction
I wrote small java program which perf= orms some selects in one transaction (and thread) and one delete in ano= ther transaction and thread on the same table holding one record initia= lly.

Environment
OS: Centos 5.5 kernel 2.6.18-194.32.1= =2Eel5
Postgresql: 9.0.4
jdbc driver: 9.0-801 JDBC 4

Results and expectations:
The program starts with= a select (SELECT transaction) and prints one record ('id' and 'address= ' from RelationAddresses table)
The program now deletes the record in= another transaction (DELETE TRANSACTION)
The program now selects= and prints the records from the SELECT transaction. (result as expecte= d one record printed)
The DELETE transaction commits.
The progr= am now selects and prints the records from the SELECT transaction. (0 r= ecords printed, we expected one record since we are still within the SE= LECT transaction which started while this record was still available)
[code]
import java.sql.Connection;
import = java.sql.DriverManager;
import java.sql.ResultSet;
import java= =2Esql.SQLException;
import java.sql.Statement;


pub= lic class TransactionTest {
    /**/
  = ;  private static final String driver =3D "org.postgresql.Driv= er";
    private static final String connection =3D = "jdbc:postgresql://192.168.0.1/sampledb";
    pr= ivate static final String username =3D "postgres";
   = private static final String password =3D "secret";
   = ; private static final String escapeChar =3D "\"";
   = /**/
   
    /*
 &nb= sp;  private static final String driver =3D "com.mysql.jdbc.Dr= iver";
    private static final String connection =3D = "jdbc:mysql://192.168.0.1/sampledb";
    priva= te static final String username =3D "root";
    privat= e static final String password =3D "secret";
    priva= te static final String escapeChar =3D "`";
    /**/    
    private TransactionTest(){=
       
    &nbs= p;   Select select =3D new Select();
    &nb= sp;   select.start();
       =
        try {
   = ;         Thread.sleep(1000);
 &nb= sp;      } catch (InterruptedException e) { /*IGNORE*/ = }
       
    &nb= sp;   Delete delete =3D new Delete();
    &n= bsp;   delete.start();
    }
 &nb= sp; 
    public static void main(String[] args) = {
        new TransactionTest();
=     }
   
    Obj= ect block =3D new Object();
    class Select extends T= hread{
        @Override
 &n= bsp;      public void run(){
    &= nbsp;       try {
     &= nbsp;          Class.forName(driver).new= Instance();
            =     Connection conn =3D DriverManager.getConnection(= connection, username, password);
       = ;         conn.setAutoCommit(false);
&n= bsp;               <= br />               =   System.out.println("------------");
     &= nbsp;          System.out.println("SELEC= T TRANSACTION: List all addresses:");
    &nbs= p;           String query =3D "SELE= CT " + escapeChar +  "relationAddressesId" + escapeChar + = ; ", " + escapeChar +  "address" + escapeChar +  " FROM "= + escapeChar +  "RelationAddresses" + escapeChar +  "";<= br />               =   Statement stmt =3D conn.createStatement();
    =             ResultSet rs =3D s= tmt.executeQuery(query);
         =        while(rs.next()){
    =                = System.out.println("relationAddressesId: " + rs.getInt(1) + " - ad= dress: " + rs.getString(2));
        &n= bsp;       }
      =           rs.close(); stmt.close();
              &nbs= p; System.out.println("------------");
      = ;         
    &nb= sp;           synchronized(block){<= br />               =       try {
       =                = ; block.wait();
          &nb= sp;         } catch (InterruptedException e) = { /*IGNORE*/}
         &nb= sp;      }
        =        
      = ;          System.out.println("---------= ---");
             = ;   System.out.println("SELECT TRANSACTION: List all addr= esses before DELETE TRANSACTION commit():");
     = ;           query =3D "SELECT " + e= scapeChar +  "relationAddressesId" + escapeChar +  ", " += escapeChar +  "address" + escapeChar +  " FROM " + escap= eChar +  "RelationAddresses" + escapeChar +  "";
&n= bsp;               s= tmt =3D conn.createStatement();
       =         rs =3D stmt.executeQuery(query);
              &nb= sp; while(rs.next()){
         &nb= sp;          System.out.println("relatio= nAddressesId: " + rs.getInt(1) + " - address: " + rs.getString(2));=
              = ;  }
            &n= bsp;   rs.close(); stmt.close();
     &= nbsp;          System.out.println("-----= -------");
            &= nbsp;  
          =       synchronized(block){
    &nb= sp;               tr= y {
             &n= bsp;          block.wait();
 =                 = ;   } catch (InterruptedException e) { /*IGNORE*/}
=                = }
             &nb= sp; 
            &= nbsp;   System.out.println("------------");
  &nb= sp;             System.out.pri= ntln("SELECT TRANSACTION: List all addresses after DELETE TRANSACTI= ON commit():");
          &nb= sp;     query =3D "SELECT " + escapeChar +  "re= lationAddressesId" + escapeChar +  ", " + escapeChar +  "= address" + escapeChar +  " FROM " + escapeChar +  "Relati= onAddresses" + escapeChar +  "";
      =           stmt =3D conn.createStatement(= );
             &nb= sp;  rs =3D stmt.executeQuery(query);
     &= nbsp;          while(rs.next()){
&= nbsp;               =     System.out.println("relationAddressesId: " + rs= =2EgetInt(1) + " - address: " + rs.getString(2));
    =             }
 &nbs= p;              rs.close(= ); stmt.close();
          &n= bsp;     System.out.println("------------");
 &nb= sp;             
&= nbsp;               = conn.commit();
          &nbs= p; } catch (ClassNotFoundException e) {
     &nbs= p;          e.printStackTrace();
&= nbsp;           } catch (SQLExcepti= on e) {
            &nbs= p;   e.printStackTrace();
      &n= bsp;     } catch (InstantiationException e) {
 &n= bsp;              e.print= StackTrace();
           = ; } catch (IllegalAccessException e) {
      = ;          e.printStackTrace();
&n= bsp;           }
  =       }
    }
  &n= bsp;
    class Delete extends Thread{
 &nb= sp;      @Override
      = ;  public void run(){
        &nbs= p;   try {
         &nbs= p;      Class.forName(driver).newInstance();
&nbs= p;               Con= nection conn =3D DriverManager.getConnection(connection, username, = password);
            &= nbsp;   conn.setAutoCommit(false);
     = ;          
  &nbs= p;             System.out.prin= tln("");
            &nb= sp;   System.out.println("DELETE TRANSACTION: Deleting ad= dresses....");
          &nbs= p;     System.out.println("");
    &nbs= p;           String query =3D "DELE= TE FROM " + escapeChar +  "RelationAddresses" + escapeChar +&n= bsp; "";
            &nb= sp;   Statement stmt =3D conn.createStatement();
 &nbs= p;              stmt.exec= uteUpdate(query);
          &= nbsp;     stmt.close();
      = ;         
    &nb= sp;           synchronized(block){<= br />               =       block.notifyAll();
     = ;           }
  &nb= sp;            
 &= nbsp;              try {<= br />               =       Thread.sleep(1000);
    &nbs= p;           } catch (InterruptedEx= ception e) {/*IGNORE*/}
         &= nbsp;      conn.commit();
    &nbs= p;          
  &nb= sp;             synchronized(b= lock){
             = ;       block.notifyAll();
   = ;             }
 &n= bsp;          } catch (ClassNotFoundExce= ption e) {
            &= nbsp;   e.printStackTrace();
      = ;      } catch (SQLException e) {
  &nb= sp;             e.printStackTr= ace();
            } cat= ch (InstantiationException e) {
       =         e.printStackTrace();
 &nb= sp;          } catch (IllegalAccessExcep= tion e) {
            &n= bsp;   e.printStackTrace();
      =       }
        }     }
}

[/code]

Expected output:
------------
SELECT TRANSACTION:= List all addresses:
relationAddressesId: 1 - address: Some address ------------

DELETE TRANSACTION: Deleting addresses....
------------
SELECT TRANSACTION: List all addresses before = DELETE TRANSACTION commit():
relationAddressesId: 1 - address: So= me address
------------
------------
SELECT TRANSACTION: = List all addresses after DELETE TRANSACTION commit():
relationAdd= ressesId: 1 - address: Some address
------------

A= ctual output:
------------
SELECT TRANSACTION: List al= l addresses:
relationAddressesId: 1 - address: Some address
---= ---------

DELETE TRANSACTION: Deleting addresses....

------------
SELECT TRANSACTION: List all addresses before DELETE T= RANSACTION commit():
relationAddressesId: 1 - address: Some addre= ss
------------
------------
SELECT TRANSACTION: List all= addresses after DELETE TRANSACTION commit():
------------

--=_b0a9ad2952644d458cabb92c0541e796--