src/main/java/org/embulk/output/SQLServerOutputPlugin.java in embulk-output-sqlserver-0.8.5 vs src/main/java/org/embulk/output/SQLServerOutputPlugin.java in embulk-output-sqlserver-0.8.6

- old
+ new

@@ -27,22 +27,23 @@ import static java.util.Locale.ENGLISH; public class SQLServerOutputPlugin extends AbstractJdbcOutputPlugin { - // for test - public static boolean preferMicrosoftDriver = true; - private static int DEFAULT_PORT = 1433; public interface SQLServerPluginTask extends PluginTask { @Config("driver_path") @ConfigDefault("null") public Optional<String> getDriverPath(); + @Config("driver_type") + @ConfigDefault("\"mssql-jdbc\"") + public String getDriverType(); + @Config("host") @ConfigDefault("null") public Optional<String> getHost(); @Config("port") @@ -90,10 +91,18 @@ public Optional<String> getNativeDriverName(); @Config("database_encoding") @ConfigDefault("\"MS932\"") public String getDatabaseEncoding(); + + @Config("connect_timeout") + @ConfigDefault("null") + public Optional<Integer> getConnectTimeout(); + + @Config("socket_timeout") + @ConfigDefault("null") + public Optional<Integer> getSocketTimeout(); } private static class UrlAndProperties { private final String url; private final Properties props; @@ -132,39 +141,34 @@ @Override protected JdbcOutputConnector getConnector(PluginTask task, boolean retryableMetadataOperation) { SQLServerPluginTask sqlServerTask = (SQLServerPluginTask) task; - boolean useJtdsDriver = false; if (sqlServerTask.getDriverPath().isPresent()) { addDriverJarToClasspath(sqlServerTask.getDriverPath().get()); + } + + boolean useJtdsDriver; + if (sqlServerTask.getDriverType().equalsIgnoreCase("mssql-jdbc")) { + useJtdsDriver = false; try { - Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); - } catch (Exception e) { - throw new ConfigException("Driver set at field 'driver_path' doesn't include Microsoft SQLServerDriver", e); + Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance(); } - } else { - boolean useMicrosoftDriver = false; - if (preferMicrosoftDriver) { - // prefer Microsoft SQLServerDriver if it is in classpath - try { - Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); - useMicrosoftDriver = true; - } catch (Exception e) { - } + catch (Exception e) { + throw new ConfigException("Can't load Microsoft SQLServerDriver from classpath", e); } - - if (!useMicrosoftDriver) { - logger.info("Using jTDS Driver"); - try { - Class.forName("net.sourceforge.jtds.jdbc.Driver"); - } catch (Exception e) { - throw new ConfigException("'driver_path' doesn't set and can't find jTDS driver", e); - } - useJtdsDriver = true; + } else if (sqlServerTask.getDriverType().equalsIgnoreCase("jtds")) { + useJtdsDriver = true; + try { + Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance(); } + catch (Exception e) { + throw new ConfigException("Can't load jTDS Driver from classpath", e); + } + } else { + throw new ConfigException("Unknown driver_type : " + sqlServerTask.getDriverType()); } UrlAndProperties urlProps = getUrlAndProperties(sqlServerTask, useJtdsDriver); logConnectionProperties(urlProps.getUrl(), urlProps.getProps()); return new SQLServerOutputConnector(urlProps.getUrl(), urlProps.getProps(), sqlServerTask.getSchema().orNull(), @@ -189,13 +193,12 @@ throw new IllegalArgumentException("Cannot set 'url' when 'insert_method' is 'native'."); } if (sqlServerTask.getHost().isPresent() || sqlServerTask.getInstance().isPresent() - || sqlServerTask.getDatabase().isPresent() - || sqlServerTask.getIntegratedSecurity().isPresent()) { - throw new IllegalArgumentException("'host', 'port', 'instance', 'database' and 'integratedSecurity' parameters are invalid if 'url' parameter is set."); + || sqlServerTask.getDatabase().isPresent()) { + throw new IllegalArgumentException("'host', 'instance' and 'database' parameters are invalid if 'url' parameter is set."); } url = sqlServerTask.getUrl().get(); } else { if (!sqlServerTask.getHost().isPresent()) { throw new IllegalArgumentException("Field 'host' is not set."); @@ -230,11 +233,15 @@ } if (!sqlServerTask.getUser().isPresent()) { throw new ConfigException("'user' option is required but not set."); } - }else { + + if (sqlServerTask.getSocketTimeout().isPresent()) { + props.setProperty("socketTimeout", String.valueOf(sqlServerTask.getSocketTimeout().get())); // seconds + } + } else { StringBuilder urlBuilder = new StringBuilder(); if (sqlServerTask.getInstance().isPresent()) { urlBuilder.append(String.format("jdbc:sqlserver://%s\\%s", sqlServerTask.getHost().get(), sqlServerTask.getInstance().get())); } else { @@ -252,10 +259,19 @@ } if (!sqlServerTask.getPassword().isPresent()) { throw new IllegalArgumentException("Field 'password' is not set."); } } + + if (sqlServerTask.getSocketTimeout().isPresent()) { + props.setProperty("socketTimeout", String.valueOf(sqlServerTask.getSocketTimeout().get() * 1000L)); // milliseconds + } + url = urlBuilder.toString(); + } + + if (sqlServerTask.getConnectTimeout().isPresent()) { + props.setProperty("loginTimeout", String.valueOf(sqlServerTask.getConnectTimeout().get())); // seconds } return new UrlAndProperties(url, props); }