Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U8CVX-0008L5-Fx for pgsql-jdbc@arkaria.postgresql.org; Wed, 20 Feb 2013 16:26:59 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1U8CVW-0002bf-1o for pgsql-jdbc@arkaria.postgresql.org; Wed, 20 Feb 2013 16:26:58 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U8CPR-00089w-2p for pgsql-jdbc@postgresql.org; Wed, 20 Feb 2013 16:20:41 +0000 Received: from host4.bluestar-software.co.uk ([195.147.17.53] helo=bluestar-software.co.uk) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U8CPK-0004ZC-04 for pgsql-jdbc@postgresql.org; Wed, 20 Feb 2013 16:20:40 +0000 Received: by bluestar-software.co.uk (CommuniGate Pro PIPE 6.0.2x) with PIPE id 1590744; Wed, 20 Feb 2013 16:20:32 +0000 X-ExtScanner: Niversoft's AddFooter filter Received: from [192.168.1.198] (account alastair.burr@bluestar-software.co.uk [192.168.1.198] verified) by bluestar-software.co.uk (CommuniGate Pro SMTP 6.0.2x) with ESMTPSA id 1590731; Wed, 20 Feb 2013 16:20:24 +0000 Message-ID: <5124F7C7.2040705@bluestar-software.co.uk> Date: Wed, 20 Feb 2013 16:20:23 +0000 From: Alastair Burr User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120713 Thunderbird/14.0 MIME-Version: 1.0 To: Dave Cramer CC: List Subject: Re: JDBC - PreparedStatement reuse oddity when using setObject() with nulls and setTimestamp() References: <5123B72D.8020002@bluestar-software.co.uk> In-Reply-To: Content-Type: multipart/alternative; boundary="------------040905090302060605080604" X-Pg-Spam-Score: -2.5 (--) 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 This is a multi-part message in MIME format. --------------040905090302060605080604 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Dave, Apologies, it looks like there is a subtelty here which I didn't realise / didn't mention before, you have to insert 4 records first before you insert the null record. I have tested with the latest driver and can now constantly reproduce this, more detail below: e.g. server statement level logging below: if you insert 3 records then a null then another timestamp: LOG: execute S_1: BEGIN LOG: execute : DELETE FROM test LOG: execute : INSERT INTO test (dt) VALUES ($1) DETAIL: parameters: $1 = '2013-02-20 22:44:14.237' LOG: execute : INSERT INTO test (dt) VALUES ($1) DETAIL: parameters: $1 = '2013-02-20 22:44:14.239' LOG: execute : INSERT INTO test (dt) VALUES ($1) DETAIL: parameters: $1 = '2013-02-20 22:44:14.24' LOG: execute : INSERT INTO test (dt) VALUES ($1) DETAIL: parameters: $1 = NULL LOG: execute S_2: INSERT INTO test (dt) VALUES ($1) DETAIL: parameters: $1 = '2013-02-20 22:44:14.241' LOG: execute S_3: COMMIT All good there, now if you insert 4 records then a null then another timestamp: LOG: execute S_1: BEGIN LOG: execute : DELETE FROM test LOG: execute : INSERT INTO test (dt) VALUES ($1) DETAIL: parameters: $1 = '2013-02-20 22:44:21.154' LOG: execute : INSERT INTO test (dt) VALUES ($1) DETAIL: parameters: $1 = '2013-02-20 22:44:21.156' LOG: execute : INSERT INTO test (dt) VALUES ($1) DETAIL: parameters: $1 = '2013-02-20 22:44:21.157' LOG: execute : INSERT INTO test (dt) VALUES ($1) DETAIL: parameters: $1 = '2013-02-20 22:44:21.157' LOG: execute S_2: INSERT INTO test (dt) VALUES ($1) DETAIL: parameters: $1 = NULL LOG: execute S_2: INSERT INTO test (dt) VALUES ($1) DETAIL: parameters: $1 = '2013-02-20' LOG: execute S_3: COMMIT And you see the time has gone, interesting to note that the it looks like this could be related to the statement numbers switching from "unnamed" to S_2? To be complete about this (and to find this subtelty) I used the following code in case you have trouble reproducing: // quick imports import java.sql.*; import java.util.*; // class to demonstrate postgresql timestamp issue with setnull // using system out printlns so can be run independantly of any framework public class DemoDateIssue { // constants private static final String POSTGRESQL_DRIVER_CLASS = "org.postgresql.Driver"; private static final String POSTGRESQL_CONNECTION_STRING = "jdbc:postgresql://host:5433/example"; private static final String POSTGRESQL_USERNAME = "example"; private static final String POSTGRESQL_PASSWORD = "example"; // main method public static void main(String[] args) { // simple create and run (new DemoDateIssue()).demo(); } // demo method public void demo() { // setup variables System.out.println("Starting demo process"); Connection tCon = null; Statement tStmt = null; PreparedStatement tPStmt = null; // try block to clean up db afterwards try { // connect to database System.out.println("Connecting to database"); Class.forName(POSTGRESQL_DRIVER_CLASS); Properties tProps = new Properties(); tProps.put("user", POSTGRESQL_USERNAME); tProps.put("password", POSTGRESQL_PASSWORD); tCon = DriverManager.getConnection(POSTGRESQL_CONNECTION_STRING, tProps); // disable auto commits to ensure this is transactional tCon.setAutoCommit(false); // assuming a simple table with one timestamp field such as "CREATE TABLE test ( dt TIMESTAMP )" // create statement to clear down data tStmt = tCon.createStatement(); tStmt.executeUpdate("DELETE FROM test"); // prepare a statement on a postgresql connection tPStmt = tCon.prepareStatement("INSERT INTO test (dt) VALUES (?) "); // note: looks like you need to insert 4 dates first before the null to cause this issue Object tObject = null; // clear parameters, set timestamp to now and execute System.out.println("Inserting first timestamp"); tPStmt.clearParameters(); tObject = new Timestamp(System.currentTimeMillis()); tPStmt.setTimestamp(1, (Timestamp)tObject); tPStmt.executeUpdate(); // clear parameters, set timestamp to now and execute System.out.println("Inserting second timestamp"); tPStmt.clearParameters(); tObject = new Timestamp(System.currentTimeMillis()); tPStmt.setTimestamp(1, (Timestamp)tObject); tPStmt.executeUpdate(); // clear parameters, set timestamp to now and execute System.out.println("Inserting third timestamp"); tPStmt.clearParameters(); tObject = new Timestamp(System.currentTimeMillis()); tPStmt.setTimestamp(1, (Timestamp)tObject); tPStmt.executeUpdate(); // clear parameters, set timestamp to now and execute System.out.println("Inserting fourth timestamp"); tPStmt.clearParameters(); tObject = new Timestamp(System.currentTimeMillis()); tPStmt.setTimestamp(1, (Timestamp)tObject); tPStmt.executeUpdate(); // clear parameters, set using a null object and execute System.out.println("Inserting null object"); tPStmt.clearParameters(); tObject = null; tPStmt.setObject(1, tObject, Types.DATE); tPStmt.executeUpdate(); // clear parameters, set timestamp to now and execute System.out.println("Inserting fifth timestamp (6th row)"); tPStmt.clearParameters(); tObject = new Timestamp(System.currentTimeMillis()); tPStmt.setTimestamp(1, (Timestamp)tObject); tPStmt.executeUpdate(); // this will output 6 rows assuming current date / time is 12/02/2013 17:08:01 // 1st row = 12/02/2013 17:08:01 // 2nd row = 12/02/2013 17:08:01 // 3rd row = 12/02/2013 17:08:01 // 4th row = 12/02/2013 17:08:01 // 5th row = null // 6th row = 12/02/2013 00:00:00 // if you only insert 3 rows before the null then it works just fine! ! // as you can see the 3rd row has had its time wiped out and set to midnight // tested against various versions, last test against PostgreSQL 9.1.3 on linux 64 bit // and using JDBC PostgreSQL 9.2devel JDBC4 (build 1000) // commit to database System.out.println("Committing to database"); tCon.commit(); // success System.out.println("Demonstration code complete, check database for results"); } catch (Exception tEx) { // bad practise to capture all exceptions but doing so for demonstrative purposes System.out.println("ERROR: An exception occurred whilst running database test, stack trace below"); tEx.printStackTrace(); } finally { // null check then close statements followed by connection if (tStmt != null) try { tStmt.close(); } catch (Exception tEx) { /* silent error in cleanup */ } if (tPStmt != null) try { tPStmt.close(); } catch (Exception tEx) { /* silent error in cleanup */ } if (tCon != null) try { tCon.close(); } catch (Exception tEx) { /* silent error in cleanup */ } } } } -- Alastair Burr Senior Engineer & Project Coordinator, Bluestar Software Telephone: +44 (0)1256 882695 Web site: www.bluestar-software.co.uk Email: alastair.burr@bluestar-software.co.uk ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ DISCLAIMER: This email message and any attachments is for the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorised review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message. The views expressed in this message may not necessarily reflect the views of Bluestar Software Ltd. Bluestar Software Ltd, Registered in England Company Registration No. 03537860, VAT No. 709 2751 29 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --------------040905090302060605080604 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Dave,

Apologies, it looks like there is a subtelty here which I didn't realise / didn't mention before, you have to insert 4 records first before you insert the null record. I have tested with the latest driver and can now constantly reproduce this, more detail below:

e.g. server statement level logging below: if you insert 3 records then a null then another timestamp:

LOG:  execute S_1: BEGIN
LOG:  execute <unnamed>: DELETE FROM test
LOG:  execute <unnamed>: INSERT INTO test (dt) VALUES ($1)
DETAIL:  parameters: $1 = '2013-02-20 22:44:14.237'
LOG:  execute <unnamed>: INSERT INTO test (dt) VALUES ($1)
DETAIL:  parameters: $1 = '2013-02-20 22:44:14.239'
LOG:  execute <unnamed>: INSERT INTO test (dt) VALUES ($1)
DETAIL:  parameters: $1 = '2013-02-20 22:44:14.24'
LOG:  execute <unnamed>: INSERT INTO test (dt) VALUES ($1)
DETAIL:  parameters: $1 = NULL
LOG:  execute S_2: INSERT INTO test (dt) VALUES ($1)
DETAIL:  parameters: $1 = '2013-02-20 22:44:14.241'
LOG:  execute S_3: COMMIT

All good there, now if you insert 4 records then a null then another timestamp:

LOG:  execute S_1: BEGIN
LOG:  execute <unnamed>: DELETE FROM test
LOG:  execute <unnamed>: INSERT INTO test (dt) VALUES ($1)
DETAIL:  parameters: $1 = '2013-02-20 22:44:21.154'
LOG:  execute <unnamed>: INSERT INTO test (dt) VALUES ($1)
DETAIL:  parameters: $1 = '2013-02-20 22:44:21.156'
LOG:  execute <unnamed>: INSERT INTO test (dt) VALUES ($1)
DETAIL:  parameters: $1 = '2013-02-20 22:44:21.157'
LOG:  execute <unnamed>: INSERT INTO test (dt) VALUES ($1)
DETAIL:  parameters: $1 = '2013-02-20 22:44:21.157'
LOG:  execute S_2: INSERT INTO test (dt) VALUES ($1)
DETAIL:  parameters: $1 = NULL
LOG:  execute S_2: INSERT INTO test (dt) VALUES ($1)
DETAIL:  parameters: $1 = '2013-02-20'
LOG:  execute S_3: COMMIT

And you see the time has gone, interesting to note that the it looks like this could be related to the statement numbers switching from "unnamed" to S_2? To be complete about this (and to find this subtelty) I used the following code in case you have trouble reproducing:

// quick imports
import java.sql.*;
import java.util.*;

// class to demonstrate postgresql timestamp issue with setnull
// using system out printlns so can be run independantly of any framework
public class DemoDateIssue
{
  // constants
  private static final String POSTGRESQL_DRIVER_CLASS = "org.postgresql.Driver";
  private static final String POSTGRESQL_CONNECTION_STRING = "jdbc:postgresql://host:5433/example";
  private static final String POSTGRESQL_USERNAME = "example";
  private static final String POSTGRESQL_PASSWORD = "example";

  // main method
  public static void main(String[] args)
  {
    // simple create and run
    (new DemoDateIssue()).demo();
  }

  // demo method
  public void demo()
  {
    // setup variables
    System.out.println("Starting demo process");
    Connection tCon = null;
    Statement tStmt = null;
    PreparedStatement tPStmt = null;

    // try block to clean up db afterwards
    try
    {
      // connect to database
      System.out.println("Connecting to database");
      Class.forName(POSTGRESQL_DRIVER_CLASS);
     
      Properties tProps = new Properties();
      tProps.put("user", POSTGRESQL_USERNAME);
      tProps.put("password", POSTGRESQL_PASSWORD);
      tCon = DriverManager.getConnection(POSTGRESQL_CONNECTION_STRING, tProps);

      // disable auto commits to ensure this is transactional
      tCon.setAutoCommit(false);
   
      // assuming a simple table with one timestamp field such as "CREATE TABLE test ( dt TIMESTAMP )"
     
      // create statement to clear down data
      tStmt = tCon.createStatement();
      tStmt.executeUpdate("DELETE FROM test");
     
      // prepare a statement on a postgresql connection
      tPStmt = tCon.prepareStatement("INSERT INTO test (dt) VALUES (?) ");

      // note: looks like you need to insert 4 dates first before the null to cause this issue
      Object tObject = null;

      // clear parameters, set timestamp to now and execute
      System.out.println("Inserting first timestamp");
      tPStmt.clearParameters();
      tObject = new Timestamp(System.currentTimeMillis());
      tPStmt.setTimestamp(1, (Timestamp)tObject);
      tPStmt.executeUpdate();
     
      // clear parameters, set timestamp to now and execute
      System.out.println("Inserting second timestamp");
      tPStmt.clearParameters();
      tObject = new Timestamp(System.currentTimeMillis());
      tPStmt.setTimestamp(1, (Timestamp)tObject);
      tPStmt.executeUpdate();
     
      // clear parameters, set timestamp to now and execute
      System.out.println("Inserting third timestamp");
      tPStmt.clearParameters();
      tObject = new Timestamp(System.currentTimeMillis());
      tPStmt.setTimestamp(1, (Timestamp)tObject);
      tPStmt.executeUpdate();
     
      // clear parameters, set timestamp to now and execute
      System.out.println("Inserting fourth timestamp");
      tPStmt.clearParameters();
      tObject = new Timestamp(System.currentTimeMillis());
      tPStmt.setTimestamp(1, (Timestamp)tObject);
      tPStmt.executeUpdate();
     
      // clear parameters, set using a null object and execute
      System.out.println("Inserting null object");
      tPStmt.clearParameters();
      tObject = null;
      tPStmt.setObject(1, tObject, Types.DATE);
      tPStmt.executeUpdate();
     
      // clear parameters, set timestamp to now and execute
      System.out.println("Inserting fifth timestamp (6th row)");
      tPStmt.clearParameters();
      tObject = new Timestamp(System.currentTimeMillis());
      tPStmt.setTimestamp(1, (Timestamp)tObject);
      tPStmt.executeUpdate();
     
      // this will output 6 rows assuming current date / time is 12/02/2013 17:08:01
      // 1st row = 12/02/2013 17:08:01
      // 2nd row = 12/02/2013 17:08:01
      // 3rd row = 12/02/2013 17:08:01
      // 4th row = 12/02/2013 17:08:01
      // 5th row = null
      // 6th row = 12/02/2013 00:00:00

      // if you only insert 3 rows before the null then it works just fine! !
     
      // as you can see the 3rd row has had its time wiped out and set to midnight
      // tested against various versions, last test against PostgreSQL 9.1.3 on linux 64 bit
      // and using JDBC PostgreSQL 9.2devel JDBC4 (build 1000)

      // commit to database
      System.out.println("Committing to database");
      tCon.commit();

      // success
      System.out.println("Demonstration code complete, check database for results");
    }
    catch (Exception tEx)
    {
      // bad practise to capture all exceptions but doing so for demonstrative purposes
      System.out.println("ERROR: An exception occurred whilst running database test, stack trace below");
      tEx.printStackTrace();
    }
    finally
    {
      // null check then close statements followed by connection
      if (tStmt != null) try { tStmt.close(); } catch (Exception tEx) { /* silent error in cleanup */ }
      if (tPStmt != null) try { tPStmt.close(); } catch (Exception tEx) { /* silent error in cleanup */ }
      if (tCon != null) try { tCon.close(); } catch (Exception tEx) { /* silent error in cleanup */ }
    }
  } 
}



-- 
Alastair Burr
Senior Engineer & Project Coordinator, Bluestar Software
Telephone: +44 (0)1256 882695
Web site: www.bluestar-software.co.uk
Email: alastair.burr@bluestar-software.co.uk


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DISCLAIMER: This email message and any attachments is for the sole
use of the intended recipient(s) and may contain confidential and
privileged information.  Any unauthorised review, use, disclosure
or distribution is prohibited. If you are not the intended recipient,
please contact the sender by reply email and destroy all copies of
the original message.

The views expressed in this message may not necessarily reflect the
views of Bluestar Software Ltd.

Bluestar Software Ltd, Registered in England
Company Registration No. 03537860, VAT No. 709 2751 29
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--------------040905090302060605080604--