提交 53cb0b94 authored 作者: Thomas Mueller's avatar Thomas Mueller

An OSGi BundleActivator is included.

上级 332cb9a8
......@@ -32,15 +32,12 @@ import org.h2.message.TraceSystem;
public class Driver implements java.sql.Driver {
private static final Driver INSTANCE = new Driver();
private static volatile boolean registered;
static {
try {
DriverManager.registerDriver(INSTANCE);
} catch (SQLException e) {
TraceSystem.traceThrowable(e);
}
load();
}
/**
* This method should not be called by an application.
*
......@@ -108,8 +105,30 @@ public class Driver implements java.sql.Driver {
/**
* INTERNAL
*/
public static Driver load() {
public static synchronized Driver load() {
try {
if (!registered) {
registered = true;
DriverManager.registerDriver(INSTANCE);
}
} catch (SQLException e) {
TraceSystem.traceThrowable(e);
}
return INSTANCE;
}
/**
* INTERNAL
*/
public static synchronized void unload() {
try {
if (registered) {
registered = false;
DriverManager.deregisterDriver(INSTANCE);
}
} catch (SQLException e) {
TraceSystem.traceThrowable(e);
}
}
}
/*
* Copyright 2004-2008 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.tools;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
/**
* The driver activator loads the H2 driver when starting the bundle.
* The driver is unloaded when stopping the bundle.
*/
public class DbDriverActivator implements BundleActivator {
public void start(BundleContext bundleContext) {
org.h2.Driver.load();
}
public void stop(BundleContext bundleContext) {
org.h2.Driver.unload();
}
}
/*
* Copyright 2004-2008 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.test.jdbc;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.h2.Driver;
import org.h2.test.TestBase;
/**
* Tests the database driver.
*/
public class TestDriver extends TestBase {
/**
* Run just this test.
*
* @param a ignored
*/
public static void main(String[] a) throws Exception {
TestBase.createCaller().init().test();
}
public void test() throws Exception {
Driver instance = Driver.load();
assertTrue(DriverManager.getDriver("jdbc:h2:~/test") == instance);
Driver.unload();
try {
java.sql.Driver d = DriverManager.getDriver("jdbc:h2:~/test");
fail(d.toString());
} catch (SQLException e) {
// ignore
}
Driver.load();
assertTrue(DriverManager.getDriver("jdbc:h2:~/test") == instance);
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论