Skip to content

Commit

Permalink
Damage control of JDBC probes.
Browse files Browse the repository at this point in the history
  • Loading branch information
fbacchella committed Apr 3, 2019
1 parent 4ffac3b commit b6c549b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
16 changes: 12 additions & 4 deletions jrds-core/src/main/java/jrds/probe/jdbc/JdbcConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import org.apache.log4j.Level;

import jrds.PropertiesManager;
import jrds.factories.ProbeBean;
import jrds.starter.Connection;

import org.apache.log4j.Level;

@ProbeBean({ "user", "password", "url", "driverClass" })
@ProbeBean({ "user", "password", "url", "driverClass"})
public class JdbcConnection extends Connection<Statement> {

private java.sql.Connection con;
Expand Down Expand Up @@ -82,9 +83,12 @@ public long setUptime() {
public boolean startConnection() {
boolean started = false;
if(getResolver().isStarted()) {
Properties p = getProperties();
p.put("user", user);
p.put("password", passwd);
try {
DriverManager.setLoginTimeout(getTimeout());
con = DriverManager.getConnection(url, user, passwd);
con = DriverManager.getConnection(url, p);
started = true;
} catch (SQLException e) {
log(Level.ERROR, e, "Sql error for %s: %s", url, e);
Expand All @@ -93,6 +97,10 @@ public boolean startConnection() {
return started;
}

private Properties getProperties() {
return new Properties();
}

@Override
public void stopConnection() {
if(con != null) {
Expand Down
7 changes: 3 additions & 4 deletions jrds-core/src/main/java/jrds/probe/jdbc/JdbcProbe.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ public abstract class JdbcProbe extends Probe<String, Number> implements UrlProb

static final void registerDriver(String JdbcDriver) {
try {
Driver jdbcDriver = (Driver) Class.forName(JdbcDriver).newInstance();
DriverManager.registerDriver(jdbcDriver);
} catch (Exception e) {
registerDriver(Class.forName(JdbcDriver));
} catch (ClassNotFoundException e) {
throw new RuntimeException("Can't register JDBC driver " + JdbcDriver, e);
}
}
Expand All @@ -38,7 +37,7 @@ static final void registerDriver(Class<?> JdbcDriver) {
try {
Driver jdbcDriver = (Driver) JdbcDriver.newInstance();
DriverManager.registerDriver(jdbcDriver);
} catch (Exception e) {
} catch (SQLException | InstantiationException | IllegalAccessException e) {
throw new RuntimeException("Can't register JDBC driver " + JdbcDriver, e);
}
}
Expand Down
2 changes: 1 addition & 1 deletion jrds-core/src/main/java/jrds/probe/jdbc/JdbcStarter.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public boolean start() {
p.put("password", passwd);
try {
DriverManager.setLoginTimeout(10);
con = DriverManager.getConnection(url, user, passwd);
con = DriverManager.getConnection(url, p);
started = true;
} catch (SQLException e) {
log(Level.ERROR, e, "SQL error: %s", e);
Expand Down
7 changes: 4 additions & 3 deletions jrds-core/src/main/java/jrds/probe/jdbc/Mysql.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public abstract class Mysql extends JdbcProbe {
private final static int PORT = 3306;

static {
registerDriver(com.mysql.jdbc.Driver.class);
registerDriver(com.mysql.cj.jdbc.Driver.class);
}

public Mysql() {
Expand Down Expand Up @@ -76,8 +76,9 @@ public String getUrlAsString() {
@Override
public Properties getProperties() {
Properties p = super.getProperties();
p.put("connectTimeout", 10000);
p.put("socketTimeout", 10000);
p.put("connectTimeout", getTimeout() * 1000);
p.put("socketTimeout", getTimeout() * 1000);
p.put("serverTimezone", "UTC");
return p;
}

Expand Down

0 comments on commit b6c549b

Please sign in to comment.