Topic
6 replies Latest Post - ‏2007-12-09T00:54:27Z by SystemAdmin
SystemAdmin
SystemAdmin
1525 Posts
ACCEPTED ANSWER

Pinned topic Programmatic restore

‏2007-11-27T17:41:49Z |
Hello,

I tried to perform a restore operation with the following code:

> public boolean restore(String from) {
> boolean bRestored = false;
> try {
> dbConnection.close(); // Close current connection.
> String dbUrl = getDatabaseUrl();
> prop.put("restoreFrom", from+"/"+dbName); // Properties
> System.out.println(dbUrl+" "+prop.get("restoreFrom"));
>// Prints: "jdbc:derby:MyDb L:/Backup1/MyDb"
> dbConnection = DriverManager.getConnection(dbUrl, prop);
> bRestored = true;
> } catch (SQLException e) {
> e.printStackTrace();
> }
> prop.remove("restoreFrom");
> return bRestored;
> }

No exception is thrown, the function returns "true", but for the rest noting happens at all.
When I enter at the console
ij> CONNECT 'jdbc:derby:MyDb;restoreFrom=L:/Backup1/MyDb';

everything works fine. So the error must be in the code.
Any ideas?
Updated on 2007-12-09T00:54:27Z at 2007-12-09T00:54:27Z by SystemAdmin
  • Stan
    Stan
    267 Posts
    ACCEPTED ANSWER

    Re: Programmatic restore

    ‏2007-12-04T22:02:51Z  in response to SystemAdmin
    Be sure that the database is shutdown prior to issuing the restore command. A good idea would be to issue the shutdown connection command just prior to issuing the restore connection command.
    • SystemAdmin
      SystemAdmin
      1525 Posts
      ACCEPTED ANSWER

      Re: Programmatic restore

      ‏2007-12-05T13:27:08Z  in response to Stan
      Hello Stan,

      thank you for your response. Actually I had tried that before.
       String dbName;

      public boolean restore(String from) {
      boolean bRestored = false;
      try {
      dbConnection.close(); // Close current connection.
      String dbUrl = getDatabaseUrl();
      prop.put("shutdown", true); // Properties
      dbConnection= DriverManager.getConnection(dbUrl, prop);
      prop.remove("shutdown");
      dbConnection.close();
      prop.put("restoreFrom", from+"/"+dbName);
      System.out.println(dbUrl+" "+prop.get("restoreFrom"));
      // Prints: "jdbc:derby:MyDb L:/Backup1/MyDb"
      dbConnection = DriverManager.getConnection(dbUrl, prop);
      bRestored = true;
      } catch (SQLException e) {
      e.printStackTrace();
      }
      prop.remove("restoreFrom");
      return bRestored;
      }
      But it's the same story: No error and no restore.
      Very strange to me.

      Regards
      Jörg
      Updated on 2007-12-05T13:27:08Z at 2007-12-05T13:27:08Z by SystemAdmin
      • Stan
        Stan
        267 Posts
        ACCEPTED ANSWER

        Re: Programmatic restore

        ‏2007-12-07T00:35:06Z  in response to SystemAdmin
        Hi

        To shutdown the DB you need quotes around true http://prop.put(\"shutdown\", \"true\") and will need to handle the confirming shutdown exception. Something like this:

        prop.put("shutdown", "true"); // Properties
        try {
        boolean gotSQLExc = false;
        try {
        dbConnection = DriverManager.getConnection(dbUrl, prop);
        } catch (SQLException se) {
        gotSQLExc = true;
        }
        if (!gotSQLExc)
        System.out.println("Database did not shut down normally");
        else
        System.out.println("Database shut down normally");
        } catch (Throwable e) {
        System.out.println("exception thrown in SHUTDOWN attempt:");
        errorPrint(e);
        }
        • SystemAdmin
          SystemAdmin
          1525 Posts
          ACCEPTED ANSWER

          Re: Programmatic restore

          ‏2007-12-07T11:25:22Z  in response to Stan
          Ooooh! How I like these errors of missing commas and quotes which, once pointed to, are so obvious. Thank you very much.
          And indeed, there is progress; for now at least an exception is thrown:
          java.sql.SQLTransientConnectionException: Database 'MyDB' shutdown.

          I read in the API:
          ... the connection operation that failed might be able to succeed when the operation is retried without any application-level changes.

          Since I even don't know what an application-level is, I think I made no changes and just repeated the line
          dbConnection= DriverManager.getConnection(dbUrl, prop);
          in the catch clause. But this retrying produces
          java.sql.SQLException: Database 'MyDB' not found.

          Still any ideas?
          • Stan
            Stan
            267 Posts
            ACCEPTED ANSWER

            Re: Programmatic restore

            ‏2007-12-07T17:41:19Z  in response to SystemAdmin
            Hi Jeorg -

            I think that all is well. The exception is expected - if you review the code snipet I sent the CATCH verifies that the Exception is thrown and prints the confirmation. The only problem is if you issue the command and do not get the Exception. It is confusing being an exception that says 'Alls well'. Refer to the description of the WwdEmbedded.java program in the Getting Started guide at:
            http://db.apache.org/derby/docs/10.3/getstart/rwwdactivity3.html
            Section: Shut down the database
            • SystemAdmin
              SystemAdmin
              1525 Posts
              ACCEPTED ANSWER

              Re: Programmatic restore

              ‏2007-12-09T00:54:27Z  in response to Stan
              Hello Stan,

              thank you very, very much indeed. Everything works fine now. Here's the code in case anybody is interested:
               public boolean restore(String from) {
              boolean bRestored= false, shutdownSuccess= false;
              SQLException ex= null;
              String dbUrl= getDatabaseUrl(); // url+dbName
              try {
              dbConnection.close(); // Close current connection.
              dbConnection= DriverManager.getConnection(dbUrl+";shutdown=true");
              }
              catch (SQLException e) {
              /* Yes! Even if it is hard to believe, but in this case a thrown exception
              signals success. See:
              http://db.apache.org/derby/docs/10.3/getstart/rwwdactivity3.html
              if (e.getSQLState().equals("XJ015")) shutdownSuccess= true; //Derby engine
              */
              if (e.getSQLState().equals("08006")) shutdownSuccess= true; // single db.
              ex= e;
              }
              if (!shutdownSuccess) {
              ex.printStackTrace();
              return false;
              }
              try {
              dbConnection.close();
              dbConnection = DriverManager.getConnection
              (dbUrl+";restoreFrom=" from"/"+dbName);
              bRestored = true;
              }
              catch (SQLException e) {
              e.printStackTrace();
              ((DonMan)parent).signalError("Error during RESTORE: ", e,
              "in: DonDao.restore", false);
              }
              connect(); // Reconnect.
              return bRestored;
              }

              /*
              The same with properties:
              ...
              try {
              dbConnection.close(); // Close current connection.
              prop.put("shutdown", "true");
              dbConnection= DriverManager.getConnection(dbUrl, prop);
              }
              catch (SQLException e) {
              if (e.getSQLState().equals("08006")) shutdownSuccess= true; // single db.
              ex= e;
              }
              if (!shutdownSuccess) {
              prop.remove("shutdown");
              ex.printStackTrace();
              return false;
              }
              try {
              prop.remove("shutdown");
              dbConnection.close();
              prop.put("restoreFrom", from+"/"+dbName);
              dbConnection = DriverManager.getConnection(dbUrl, prop);
              bRestored = true;
              }
              catch (SQLException e) {
              e.printStackTrace();
              ((DonMan)parent).signalError("Error during RESTORE: ", e,
              "in: DonDao.restore", false);
              }
              prop.remove("restoreFrom");
              connect(); // Reconnect.
              return bRestored;
              }
              */
              Updated on 2007-12-09T00:54:27Z at 2007-12-09T00:54:27Z by SystemAdmin