Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.Driver;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Enumeration;
- import java.util.logging.LogManager;
- import org.apache.derby.iapi.services.monitor.Monitor;
- public class Main {
- private Connection con;
- public Main() {
- }
- public void start() throws Exception {
- System.out.println("Starting");
- Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
- con = DriverManager.getConnection("jdbc:derby:db");
- // Class.forName("org.hsqldb.jdbc.JDBCDriver");
- // con = DriverManager.getConnection("jdbc:hsqldb:file:mymemdb;shutdown=true", "SA", "");
- try {
- // If we comment the following lines and don't run the statements the
- // leak does not happen
- Statement stmt = con.createStatement();
- stmt.execute("CREATE TABLE TEST (A VARCHAR(12))");
- stmt.execute("DROP TABLE TEST");
- stmt.close();
- stmt = null;
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void stop() throws Exception {
- System.out.println("Stopping");
- try {
- con.close();
- con = null;
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- try {
- DriverManager.getConnection("jdbc:derby:db;shutdown=true");
- } catch (SQLException e) {
- if ("Database 'db' shutdown.".equals(e.getMessage()) || "Database 'db' not found.".equals(e.getMessage())) {
- // ignore, we're shutting down
- } else {
- throw e;
- }
- }
- try {
- DriverManager.getConnection("jdbc:derby:;shutdown=true");
- } catch (SQLException e) {
- if ("Derby system shutdown.".equals(e.getMessage()) || "Database 'db' not found.".equals(e.getMessage())) {
- // ignore, we're shutting down
- } else {
- throw e;
- }
- }
- // This shuts down the derby.antiGC thread
- Monitor.getMonitor().shutdown();
- // Ensure that we unload all JDBC drivers
- Enumeration<Driver> enumeration = DriverManager.getDrivers();
- while(enumeration.hasMoreElements()) {
- Driver driver = enumeration.nextElement();
- DriverManager.deregisterDriver(driver);
- }
- LogManager.getLogManager().reset();
- }
- @Override
- protected void finalize() throws Throwable {
- System.out.println("******** Main just got finalized!");
- super.finalize();
- }
- }
- import java.io.File;
- import java.lang.reflect.Method;
- import java.net.URL;
- public class Test {
- public static void main(String[] args) throws Exception {
- URL[] urls = new URL[] {
- new File("../Test1/bin/").toURI().toURL(),
- new File("../Test1/lib/derby.jar").toURI().toURL(),
- // new File("../Test1/lib/javadb.jar").toURI().toURL(),
- new File("../Test1/lib/hsqldb.jar").toURI().toURL(),
- };
- ClassLoader classLoader = new TestClassloader(urls);
- Class<?> clz = classLoader.loadClass("Main");
- Object o = clz.newInstance();
- try {
- Method m = o.getClass().getMethod("start");
- m.invoke(o);
- m = null;
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- try {
- Method m = o.getClass().getMethod("stop");
- m.invoke(o);
- m = null;
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- o = null;
- clz = null;
- classLoader = null;
- while (true) {
- System.gc();
- Thread.yield();
- }
- }
- }
- import java.net.URL;
- import java.net.URLClassLoader;
- import java.net.URLStreamHandlerFactory;
- public class TestClassloader extends URLClassLoader {
- public TestClassloader(URL[] urls) {
- super(urls);
- // TODO Auto-generated constructor stub
- }
- public TestClassloader(URL[] urls, ClassLoader parent) {
- super(urls, parent);
- // TODO Auto-generated constructor stub
- }
- public TestClassloader(URL[] urls, ClassLoader parent,
- URLStreamHandlerFactory factory) {
- super(urls, parent, factory);
- // TODO Auto-generated constructor stub
- }
- @Override
- protected void finalize() throws Throwable {
- System.out.println("******** TestClassloader just got finalized!");
- super.finalize();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement