Skip to content

Commit

Permalink
Add | Functions to register/deregister the driver, deregistering the …
Browse files Browse the repository at this point in the history
…driver also attempts to stop the timeout poller
  • Loading branch information
rene-ye committed Dec 18, 2018
1 parent e0d9ced commit 6deabc4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
25 changes: 24 additions & 1 deletion src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -548,16 +548,39 @@ String getClassNameLogging() {

private final static java.util.logging.Logger drLogger = java.util.logging.Logger
.getLogger("com.microsoft.sqlserver.jdbc.internals.SQLServerDriver");
private static java.sql.Driver mssqlJdbcDriver = null;

// Register with the DriverManager
static {
try {
java.sql.DriverManager.registerDriver(new SQLServerDriver());
register();
} catch (SQLException e) {
if (drLogger.isLoggable(Level.FINER) && Util.IsActivityTraceOn()) {
drLogger.finer("Error registering driver: " + e);
}
}
}

/*
* Registers the driver with the DriverManager. This function is a no-op if there is already an instance of the Driver registered.
*/
public static void register() throws SQLException {
if (mssqlJdbcDriver == null) {
mssqlJdbcDriver = new SQLServerDriver();
DriverManager.registerDriver(mssqlJdbcDriver);
}
}

/*
* De-registers the driver from the DriverManager. This function is a no-op if there isn't already an instance of the Driver registered.
*/
public static void deRegister() throws SQLException {
if (mssqlJdbcDriver != null) {
DriverManager.deregisterDriver(mssqlJdbcDriver);
TimeoutPoller.getTimeoutPoller().requestStop();
mssqlJdbcDriver = null;
}
}

public SQLServerDriver() {
instanceID = nextInstanceID();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ final class TimeoutPoller implements Runnable {
private List<TimeoutCommand<TDSCommand>> timeoutCommands = new ArrayList<>();
final static Logger logger = Logger.getLogger("com.microsoft.sqlserver.jdbc.TimeoutPoller");
private static volatile TimeoutPoller timeoutPoller = null;
private boolean requestedStop = false;

static TimeoutPoller getTimeoutPoller() {
if (timeoutPoller == null) {
Expand All @@ -37,6 +38,12 @@ static TimeoutPoller getTimeoutPoller() {
return timeoutPoller;
}

void requestStop() {
if (!requestedStop) {
requestedStop = true;
}
}

void addTimeoutCommand(TimeoutCommand<TDSCommand> timeoutCommand) {
synchronized (timeoutCommands) {
timeoutCommands.add(timeoutCommand);
Expand All @@ -55,7 +62,7 @@ public void run() {
try {
// Poll every second checking for commands that have timed out and need
// interruption
while (true) {
while (!requestedStop) {
synchronized (timeoutCommands) {
Iterator<TimeoutCommand<TDSCommand>> timeoutCommandIterator = timeoutCommands.iterator();
while (timeoutCommandIterator.hasNext()) {
Expand Down
11 changes: 3 additions & 8 deletions src/main/java/com/microsoft/sqlserver/jdbc/osgi/Activator.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*/
package com.microsoft.sqlserver.jdbc.osgi;

import java.sql.DriverManager;
import java.util.Dictionary;
import java.util.Hashtable;

Expand All @@ -22,12 +21,11 @@
public class Activator implements BundleActivator {

private ServiceRegistration<DataSourceFactory> service;
SQLServerDriver driver;

@Override
public void start(BundleContext context) throws Exception {
Dictionary<String, Object> properties = new Hashtable<>();
driver = new SQLServerDriver();
java.sql.Driver driver = new SQLServerDriver();
properties.put(DataSourceFactory.OSGI_JDBC_DRIVER_CLASS, driver.getClass().getName());
properties.put(DataSourceFactory.OSGI_JDBC_DRIVER_NAME, "Microsoft JDBC Driver for SQL Server");
properties.put(DataSourceFactory.OSGI_JDBC_DRIVER_VERSION,
Expand All @@ -40,10 +38,7 @@ public void stop(BundleContext context) throws Exception {
if (service != null) {
service.unregister();
}

if (driver != null) {
DriverManager.deregisterDriver(driver);
driver = null;
}

SQLServerDriver.deRegister();
}
}

0 comments on commit 6deabc4

Please sign in to comment.