Github user fhueske commented on a diff in the pull request:

    https://github.com/apache/flink/pull/1941#discussion_r62832299
  
    --- Diff: 
flink-batch-connectors/flink-jdbc/src/main/java/org/apache/flink/api/java/io/jdbc/JDBCInputFormat.java
 ---
    @@ -19,59 +19,112 @@
     package org.apache.flink.api.java.io.jdbc;
     
     import java.io.IOException;
    +import java.math.BigDecimal;
    +import java.sql.Array;
     import java.sql.Connection;
    +import java.sql.Date;
     import java.sql.DriverManager;
    +import java.sql.PreparedStatement;
     import java.sql.ResultSet;
     import java.sql.ResultSetMetaData;
     import java.sql.SQLException;
    -import java.sql.Statement;
    -
    -import org.apache.flink.api.common.io.NonParallelInput;
    -import org.slf4j.Logger;
    -import org.slf4j.LoggerFactory;
    +import java.sql.Time;
    +import java.sql.Timestamp;
    +import java.util.Arrays;
     
     import org.apache.flink.api.common.io.DefaultInputSplitAssigner;
     import org.apache.flink.api.common.io.RichInputFormat;
     import org.apache.flink.api.common.io.statistics.BaseStatistics;
    -import org.apache.flink.api.java.tuple.Tuple;
    +import org.apache.flink.api.java.io.jdbc.split.ParameterValuesProvider;
    +import org.apache.flink.api.table.Row;
     import org.apache.flink.configuration.Configuration;
     import org.apache.flink.core.io.GenericInputSplit;
     import org.apache.flink.core.io.InputSplit;
     import org.apache.flink.core.io.InputSplitAssigner;
    -import org.apache.flink.types.NullValue;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
     
     /**
    - * InputFormat to read data from a database and generate tuples.
    + * InputFormat to read data from a database and generate Rows.
      * The InputFormat has to be configured using the supplied 
InputFormatBuilder.
      * 
    - * @param <OUT>
    - * @see Tuple
    + * In order to query the JDBC source in parallel, you need to provide a 
parameterized
    + * query template (i.e. a valid {@link PreparedStatement}) and a {@link 
ParameterValuesProvider} 
    + * which provides binding values for the query parameters.
    + * 
    + * @see Row
    + * @see ParameterValuesProvider
    + * @see PreparedStatement
      * @see DriverManager
      */
    -public class JDBCInputFormat<OUT extends Tuple> extends 
RichInputFormat<OUT, InputSplit> implements NonParallelInput {
    -   private static final long serialVersionUID = 1L;
    +public class JDBCInputFormat extends RichInputFormat<Row, InputSplit> {
     
    +   private static final long serialVersionUID = 1L;
        private static final Logger LOG = 
LoggerFactory.getLogger(JDBCInputFormat.class);
     
        private String username;
        private String password;
        private String drivername;
        private String dbURL;
    -   private String query;
    +   private String queryTemplate;
        private int resultSetType;
        private int resultSetConcurrency;
     
        private transient Connection dbConn;
    -   private transient Statement statement;
    +   private transient PreparedStatement statement;
        private transient ResultSet resultSet;
    -
    -   private int[] columnTypes = null;
    -
    +   
    +   private boolean hasNext = true;
    +   private Object[][] parameterValues;
    +   
        public JDBCInputFormat() {
        }
     
        @Override
        public void configure(Configuration parameters) {
    +           //do nothing here
    +   }
    +   
    +   @Override
    +   public void openInputFormat() {
    +           //called once per inputFormat (on open)
    +           try {
    +                   Class.forName(drivername);
    +                   if (username == null) {
    +                           dbConn = DriverManager.getConnection(dbURL);
    +                   } else {
    +                           dbConn = DriverManager.getConnection(dbURL, 
username, password);
    +                   }
    +                   statement = dbConn.prepareStatement(queryTemplate, 
resultSetType, resultSetConcurrency);
    +           } catch (SQLException se) {
    +                   throw new IllegalArgumentException("open() failed." + 
se.getMessage(), se);
    +           } catch (ClassNotFoundException cnfe) {
    +                   throw new IllegalArgumentException("JDBC-Class not 
found. - " + cnfe.getMessage(), cnfe);
    +           }
    +   }
    +   
    +   @Override
    +   public void closeInputFormat() {
    +           //called once per inputFormat (on close)
    +           try {
    +                   statement.close();
    +           } catch (SQLException se) {
    +                   LOG.info("Inputformat Statement couldn't be closed - " 
+ se.getMessage());
    +           } catch (NullPointerException npe) {
    +           } finally {
    +                   statement = null;
    +           }
    +           
    +           try {
    +                   dbConn.close();
    --- End diff --
    
    Check for `dbConn == null`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to