/*
 * The MIT License
 *
 *   Copyright (c) 2014, benas (md.benhassine@gmail.com)
 *
 *   Permission is hereby granted, free of charge, to any person obtaining a copy
 *   of this software and associated documentation files (the "Software"), to deal
 *   in the Software without restriction, including without limitation the rights
 *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *   copies of the Software, and to permit persons to whom the Software is
 *   furnished to do so, subject to the following conditions:
 *
 *   The above copyright notice and this permission notice shall be included in
 *   all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *   THE SOFTWARE.
 */

package io.github.benas.easybatch.jdbc;

import io.github.benas.easybatch.core.api.RecordReader;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * A {@link io.github.benas.easybatch.core.api.RecordReader} that reads records from a database using jdbc API.
 *
 * @author benas (md.benhassine@gmail.com)
 */
public class JdbcRecordReader implements RecordReader {

    /**
     * The database connection to use to read data.
     */
    private Connection connection;

    /**
     * The statement to use to read data.
     */
    private Statement statement;

    /**
     * The result set that will be returned.
     */
    private ResultSet resultSet;

    /**
     * The jdbc query to use to fetch data.
     */
    private String query;

    /**
     * The current record number.
     */
    private long currentRecordNumber;

    /**
     * Create a JdbcRecordReader instance.
     * @param connection the connection to use to read data
     * @param query the jdbc query to use to fetch data
     */
    public JdbcRecordReader(final Connection connection, final String query) {
        this.connection = connection;
        this.query = query;
    }

    @Override
    public void open() throws Exception {
        currentRecordNumber = 0;
        statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        resultSet = statement.executeQuery(query);
    }

    @Override
    public boolean hasNextRecord() {
        try {
            return resultSet.next();
        } catch (SQLException e) {
            throw new RuntimeException("An exception occurred during reading database next record", e);
        }
    }

    @Override
    public JdbcRecord readNextRecord() {
        return new JdbcRecord(++currentRecordNumber, resultSet);
    }

    @Override
    public Long getTotalRecords() {
        long rowCount = 0;

        try {
            if (resultSet.last()) {
                rowCount = resultSet.getRow();
                resultSet.beforeFirst();
            }
            return rowCount;
        } catch (SQLException e) {
            throw new RuntimeException("An exception occurred during fetching database record set size", e);
        }
    }

    @Override
    public String getDataSourceName() {
        try {
            return "Connection URL: " + connection.getMetaData().getURL() + " | " +
                    "Query string: " + query;
        } catch (SQLException e) {
            throw new RuntimeException("Unable to get data source name", e);
        }
    }

    @Override
    public void close() {
        try {
            if (resultSet != null) {
                resultSet.close();
            }
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException("An exception occurred during database connection closing", e);
        }
    }

}

@@ -1394,16 +1394,81 @@
 atement;
+%0Aimport java.util.logging.Level;%0Aimport java.util.logging.Logger;
 %0A%0A/**%0A *
@@ -1684,16 +1684,158 @@
 ader %7B%0A%0A
+    /**%0A     * The logger to use.%0A     */%0A    private static final Logger LOGGER = Logger.getLogger(JdbcRecordReader.class.getSimpleName());%0A%0A
     /**%0A
@@ -3031,35 +3031,33 @@
     
-throw new RuntimeException(
+LOGGER.log(Level.SEVERE, 
 %22An 
@@ -3086,41 +3086,85 @@
 ing 
-reading database next record%22, e)
+checking the existence of next database record%22, e);%0A            return false
 ;%0A  
@@ -3615,35 +3615,33 @@
     
-throw new RuntimeException(
+LOGGER.log(Level.SEVERE, 
 %22An 
@@ -3698,32 +3698,57 @@
  set size%22, e);%0A
+            return null;%0A
         %7D%0A    %7D%0A
@@ -3997,35 +3997,33 @@
     
-throw new RuntimeException(
+LOGGER.log(Level.SEVERE, 
 %22Una
@@ -4048,32 +4048,58 @@
 urce name%22, e);%0A
+            return %22N/A%22;%0A
         %7D%0A    %7D%0A
@@ -4137,36 +4137,35 @@
 close() 
-%7B%0A        try %7B%0A    
+throws Exception %7B%0A
         
@@ -4193,36 +4193,32 @@
 ) %7B%0A            
-    
 resultSet.close(
@@ -4224,34 +4224,26 @@
 ();%0A        
-  
-  %7D%0A    
+%7D%0A
         if (
@@ -4271,28 +4271,24 @@
             
-   
- 
 statement.cl
@@ -4298,34 +4298,26 @@
 ();%0A        
-    %7D%0A    
+%7D%0A
         if (
@@ -4350,20 +4350,16 @@
         
-    
 connecti
@@ -4374,160 +4374,8 @@
 ();%0A
-            %7D%0A        %7D catch (SQLException e) %7B%0A            throw new RuntimeException(%22An exception occurred during database connection closing%22, e);%0A
     

Improve JdbcRecordReader implementation : when an SqlException occurs, do not throw a RuntimeException (that cannot be handled correctly by the engine). Instead, log an error message and return an appropriate result.