Advertisement
Guest User

Untitled

a guest
May 19th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 4.19 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.Driver;
  3. import java.sql.DriverManager;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import java.util.Enumeration;
  7. import java.util.logging.LogManager;
  8.  
  9. import org.apache.derby.iapi.services.monitor.Monitor;
  10.  
  11.  
  12. public class Main {
  13.     private Connection con;
  14.    
  15.     public Main() {
  16.     }
  17.    
  18.     public void start() throws Exception {
  19.         System.out.println("Starting");
  20.        
  21.         Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
  22.         con = DriverManager.getConnection("jdbc:derby:db");
  23.        
  24. //      Class.forName("org.hsqldb.jdbc.JDBCDriver");
  25. //      con = DriverManager.getConnection("jdbc:hsqldb:file:mymemdb;shutdown=true", "SA", "");
  26.        
  27.         try {
  28.             // If we comment the following lines and don't run the statements the
  29.             // leak does not happen
  30.             Statement stmt = con.createStatement();
  31.             stmt.execute("CREATE TABLE TEST (A VARCHAR(12))");
  32.             stmt.execute("DROP TABLE TEST");
  33.             stmt.close();
  34.             stmt = null;
  35.         }
  36.         catch (Exception e) {
  37.             e.printStackTrace();
  38.         }
  39.     }
  40.    
  41.     public void stop() throws Exception {
  42.         System.out.println("Stopping");
  43.        
  44.         try {
  45.             con.close();
  46.             con = null;
  47.         }
  48.         catch (Exception e) {
  49.             e.printStackTrace();
  50.         }
  51.        
  52.         try {
  53.             DriverManager.getConnection("jdbc:derby:db;shutdown=true");
  54.         } catch (SQLException e) {
  55.             if ("Database 'db' shutdown.".equals(e.getMessage()) || "Database 'db' not found.".equals(e.getMessage())) {
  56.                 // ignore, we're shutting down
  57.             } else {
  58.                 throw e;
  59.             }
  60.         }
  61.         try {
  62.             DriverManager.getConnection("jdbc:derby:;shutdown=true");
  63.         } catch (SQLException e) {
  64.             if ("Derby system shutdown.".equals(e.getMessage()) || "Database 'db' not found.".equals(e.getMessage())) {
  65.                 // ignore, we're shutting down
  66.             } else {
  67.                 throw e;
  68.             }
  69.         }
  70.        
  71.         // This shuts down the derby.antiGC thread
  72.         Monitor.getMonitor().shutdown();
  73.        
  74.         // Ensure that we unload all JDBC drivers
  75.         Enumeration<Driver> enumeration = DriverManager.getDrivers();
  76.  
  77.         while(enumeration.hasMoreElements()) {
  78.             Driver driver = enumeration.nextElement();
  79.             DriverManager.deregisterDriver(driver);
  80.         }
  81.  
  82.         LogManager.getLogManager().reset();
  83.     }
  84.    
  85.     @Override
  86.     protected void finalize() throws Throwable {
  87.         System.out.println("******** Main just got finalized!");
  88.         super.finalize();
  89.     }
  90. }
  91.  
  92. import java.io.File;
  93. import java.lang.reflect.Method;
  94. import java.net.URL;
  95.  
  96. public class Test {
  97.     public static void main(String[] args) throws Exception {
  98.         URL[] urls = new URL[] {
  99.             new File("../Test1/bin/").toURI().toURL(),
  100.             new File("../Test1/lib/derby.jar").toURI().toURL(),
  101. //          new File("../Test1/lib/javadb.jar").toURI().toURL(),
  102.             new File("../Test1/lib/hsqldb.jar").toURI().toURL(),
  103.         };
  104.         ClassLoader classLoader = new TestClassloader(urls);
  105.        
  106.         Class<?> clz = classLoader.loadClass("Main");
  107.        
  108.         Object o = clz.newInstance();
  109.        
  110.         try {
  111.             Method m = o.getClass().getMethod("start");
  112.             m.invoke(o);
  113.             m = null;
  114.         }
  115.         catch (Exception e) {
  116.             e.printStackTrace();
  117.         }
  118.        
  119.         try {
  120.             Method m = o.getClass().getMethod("stop");
  121.             m.invoke(o);
  122.             m = null;
  123.         }
  124.         catch (Exception e) {
  125.             e.printStackTrace();
  126.         }
  127.        
  128.         o = null;
  129.        
  130.         clz = null;
  131.        
  132.         classLoader = null;
  133.        
  134.         while (true) {
  135.             System.gc();
  136.             Thread.yield();
  137.         }
  138.     }
  139. }
  140.  
  141.  
  142. import java.net.URL;
  143. import java.net.URLClassLoader;
  144. import java.net.URLStreamHandlerFactory;
  145.  
  146.  
  147. public class TestClassloader extends URLClassLoader {
  148.  
  149.     public TestClassloader(URL[] urls) {
  150.         super(urls);
  151.         // TODO Auto-generated constructor stub
  152.     }
  153.  
  154.     public TestClassloader(URL[] urls, ClassLoader parent) {
  155.         super(urls, parent);
  156.         // TODO Auto-generated constructor stub
  157.     }
  158.  
  159.     public TestClassloader(URL[] urls, ClassLoader parent,
  160.             URLStreamHandlerFactory factory) {
  161.         super(urls, parent, factory);
  162.         // TODO Auto-generated constructor stub
  163.     }
  164.  
  165.     @Override
  166.     protected void finalize() throws Throwable {
  167.         System.out.println("******** TestClassloader just got finalized!");
  168.         super.finalize();
  169.     }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement