Uploaded image for project: 'Cayenne'
  1. Cayenne
  2. CAY-1264

Shutdown embedded database after scheme generation by maven-cayenne-plugin

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 3.0M6
    • Undefined future
    • Core Library
    • None

    Description

      Using maven-cayenne-plugin for Derby embedded database scheme generation for unit test with Pax Exam lead to exception in test: "Another instance of Derby might have already booted the database". This is because Derby database already loaded by JVM running maven-cayenne-plugin, and same database cannot be loaded by Pax Exam test running in isolated JVM through RMI.
      Patch below trying to shutdowns (embedded) database after scheme population:

      Index: src/main/java/org/apache/cayenne/tools/DbGeneratorMojo.java
      ===================================================================
      — src/main/java/org/apache/cayenne/tools/DbGeneratorMojo.java (revision 807961)
      +++ src/main/java/org/apache/cayenne/tools/DbGeneratorMojo.java (working copy)
      @@ -34,6 +34,10 @@

      import java.io.File;
      import java.sql.Driver;
      +import java.sql.DriverManager;
      +import java.sql.SQLException;
      +import java.util.Enumeration;
      +import java.util.Properties;

      /**

      • Maven mojo to perform class generation from data map. This class is a Maven
        @@ -139,6 +143,7 @@
        logger.info(String.format("generator options - [dropTables: %s, dropPK: %s, createTables: %s, createPK: %s, createFK: %s]",
        dropTables, dropPK, createTables, createPK, createFK));

      + Driver registeredDriver = null;
      try

      { final DbAdapter adapterInst = (adapter == null) ? new JdbcAdapter() : (DbAdapter) Class.forName(adapter).newInstance(); @@ -153,8 +158,9 @@ generator.setShouldDropTables(dropTables); // load driver taking custom CLASSPATH into account... - DriverDataSource dataSource = new DriverDataSource((Driver) Class.forName( - driver).newInstance(), url, username, password); + registeredDriver = (Driver) Class.forName(driver).newInstance(); + DriverDataSource dataSource = new DriverDataSource(registeredDriver, + url, username, password); generator.runGenerator(dataSource); }

      @@ -170,6 +176,29 @@
      logger.error(message);
      throw new MojoExecutionException(message, th);
      }
      + // Shutdown embedded database
      + if (registeredDriver != null) {
      + Properties shutdownProperties = new Properties();
      + shutdownProperties.put("shutdown", "true");
      + try

      { + registeredDriver.connect(url, shutdownProperties).close(); + }

      catch (SQLException e)

      { + logger.debug(String.format("Shutdown %s: %s", driver, e.getMessage())); + }

      + }
      + // Unregister drivers
      + Enumeration<Driver> drivers = DriverManager.getDrivers();
      + while (drivers.hasMoreElements()) {
      + registeredDriver = drivers.nextElement();
      + try {
      + if (registeredDriver.acceptsURL(url))

      { + DriverManager.deregisterDriver(registeredDriver); + }

      + logger.debug("Unregistered driver " + registeredDriver);
      + } catch (SQLException e)

      { + logger.error("Can't unregister driver " + registeredDriver, e); + }

      + }
      }

      /** Loads and returns DataMap based on <code>map</code> attribute. */

      Attachments

        Activity

          People

            Unassigned Unassigned
            kapyar Victor Antonovich
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: