Skip to content

java 8 date support in TaskExecution breaks DB2 support #948

@Cuball0

Description

@Cuball0

The implementation of this commit : a0fda4b

Breaks DB2 LUW support.

return new TaskExecution(id, getNullableExitCode(rs), rs.getString("TASK_NAME"),
					rs.getTimestamp("START_TIME"), rs.getTimestamp("END_TIME"), rs.getString("EXIT_MESSAGE"),

The code above was changed into the code below: (rs.getTimestamp("field") -> rs.getObject("field",LocalDateTime.class)

return new TaskExecution(id, getNullableExitCode(rs), rs.getString("TASK_NAME"),
					rs.getObject("START_TIME", LocalDateTime.class), rs.getObject("END_TIME", LocalDateTime.class),

The latter however throws and exception when the column has a is null value (which is the case for START_TIME and END_TIME at the start of a task excecution. Because of this, it's impossible to run spring-cloud tasks anymore that use DB2 as a database.

Simple test to reproduce the problem with DB2 driver

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.LocalDateTime;


public class DB2Test {

    public static void main(String[] args) {
        // Replace with your actual database connection details
        String dbUrl = "jdbc:db2://server.name:50012/DB";
        String dbUsername = "user";
        String dbPassword = "pass";

        var tableName = "TEST";

        String drop = "DROP TABLE IF EXISTS " + tableName;
        String create = "CREATE TABLE " + tableName + "  ( ID BIGINT, YOUR_TIMESTAMP_COLUMN TIMESTAMP(9) )";
        String insert1 = "INSERT INTO " + tableName + "( ID, YOUR_TIMESTAMP_COLUMN) VALUES ( 0,  current_timestamp )";
        String insert2 = "INSERT INTO " + tableName + "( ID ) VALUES ( 1 )";

        String query = "SELECT YOUR_TIMESTAMP_COLUMN FROM " + tableName;


        try (Connection connection = DriverManager.getConnection(dbUrl, dbUsername, dbPassword);
        ) {
            PreparedStatement dropStatement = connection.prepareStatement(drop);
            PreparedStatement createStatement = connection.prepareStatement(create);
            PreparedStatement insertStatement1 = connection.prepareStatement(insert1);
            PreparedStatement insertStatement2 = connection.prepareStatement(insert2);
            PreparedStatement queryStatement = connection.prepareStatement(query);


            dropStatement.executeUpdate();
            createStatement.executeUpdate();
            insertStatement1.executeUpdate();
            insertStatement2.executeUpdate();

            var resultSet = queryStatement.executeQuery();


            while (resultSet.next()) {

                System.out.println("------ ROW " + resultSet.getRow() + " -------");
                //via
                try {
                    LocalDateTime localDateTime = resultSet.getObject("YOUR_TIMESTAMP_COLUMN", LocalDateTime.class);
                    System.out.println("Successful retrieved LocalDateTime : " + localDateTime);
                } catch (Exception e) {
                    System.out.println("Unable to retrieve YOUR_TIMESTAMP_COLUMN via getObject(YOUR_TIMESTAMP_COLUMN, LocalDateTime.class)");
                    System.out.println("Error message: " + e.getMessage());
                }


                try {
                    var yourTimestampColumn = resultSet.getTimestamp("YOUR_TIMESTAMP_COLUMN");

                    java.util.Date dateFromTimestamp = (yourTimestampColumn != null) ? (java.util.Date) yourTimestampColumn.clone() : null;

                    System.out.println("Successful retrieved Date from Timestamp : " + dateFromTimestamp);
                } catch (Exception e) {
                    System.out.println("Unable to retrieve YOUR_TIMESTAMP_COLUMN via getTimestamp(YOUR_TIMESTAMP_COLUMN)");
                    System.out.println("Error message: " + e.getMessage());

                }

            }

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("Error executing query: " + e.getMessage());
        }
    }
}

Output :

------ RECORD -------1
Successful retrieved LocalDateTime : 2023-12-22T08:16:50.674910
Successful retrieved Date from Timestamp : 2023-12-22 08:16:50.67491
------ RECORD -------2
Unable to retrieve YOUR_TIMESTAMP_COLUMN via getObject(YOUR_TIMESTAMP_COLUMN, LocalDateTime.class)
Error message: Cannot invoke "java.sql.Timestamp.toLocalDateTime()" because "" is null
Successful retrieved Date from Timestamp : null

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions