What is the proper way to shutdown a specific db and close the connection?












1














I created the following three methods but am confused about what shutDownDB() is really doing. Here are my questions:




  1. Why do I need an active connection to shutdown a specific database?

  2. Doesn't it make more sense to close the connection and then shutdown the DB

  3. Why do I need to close a connection if the specific database is shutdown?

  4. How come I'm able to reestablish a connection after the specific db has been shut down?

  5. What is the actual difference between shutting down a specific db and closing the connection?


NOTE: using derby in embedded mode



    public static Connection openDB(String dbFolderString) {
Connection conn = null;
try{
File dbFolder = new File(dbFolderString);
String URL = "jdbc:derby:" + dbFolderString + ";create=true";
if(print)System.out.println("n" + "db exists " + dbFolder.exists());

conn = DriverManager.getConnection(URL);
if(print)System.out.println("Succesfully connected to " + dbFolderString);
}catch(SQLException e){
System.out.println("FATAL ERROR: from getDB " + e);
System.exit(0);
}
return conn;
}

public static boolean shutDownDB(Connection conn) {
//shutsdown a specific database but DOES NOT SHUTDOWN DERBY
try{
String tokens;
String url = conn.getMetaData().getURL();
tokens = url.split(":");
DriverManager.getConnection("jdbc:derby:" + tokens[2] +";shutdown=true");
}catch(SQLException e1){
if(e1.getSQLState().equals("08006") && e1.getErrorCode() == 45000){
if(false)System.out.println(e1.getSQLState() + " " + e1.getErrorCode());
if(print)System.out.println("n" + "Database shutdown successful");
}else{
System.out.println(e1.getSQLState() + " " + e1.getErrorCode());
System.out.println("FATAL ERROR: Database not shutdown " + e1);
System.exit(0);
}
}
return true;
}

public static void closeConnection(Connection conn){
try{
conn.close();
if(print)System.out.println("Connection closed");
}catch(SQLException e){
System.out.println("connection NOT closed " + e);
System.exit(0);
}
}









share|improve this question





























    1














    I created the following three methods but am confused about what shutDownDB() is really doing. Here are my questions:




    1. Why do I need an active connection to shutdown a specific database?

    2. Doesn't it make more sense to close the connection and then shutdown the DB

    3. Why do I need to close a connection if the specific database is shutdown?

    4. How come I'm able to reestablish a connection after the specific db has been shut down?

    5. What is the actual difference between shutting down a specific db and closing the connection?


    NOTE: using derby in embedded mode



        public static Connection openDB(String dbFolderString) {
    Connection conn = null;
    try{
    File dbFolder = new File(dbFolderString);
    String URL = "jdbc:derby:" + dbFolderString + ";create=true";
    if(print)System.out.println("n" + "db exists " + dbFolder.exists());

    conn = DriverManager.getConnection(URL);
    if(print)System.out.println("Succesfully connected to " + dbFolderString);
    }catch(SQLException e){
    System.out.println("FATAL ERROR: from getDB " + e);
    System.exit(0);
    }
    return conn;
    }

    public static boolean shutDownDB(Connection conn) {
    //shutsdown a specific database but DOES NOT SHUTDOWN DERBY
    try{
    String tokens;
    String url = conn.getMetaData().getURL();
    tokens = url.split(":");
    DriverManager.getConnection("jdbc:derby:" + tokens[2] +";shutdown=true");
    }catch(SQLException e1){
    if(e1.getSQLState().equals("08006") && e1.getErrorCode() == 45000){
    if(false)System.out.println(e1.getSQLState() + " " + e1.getErrorCode());
    if(print)System.out.println("n" + "Database shutdown successful");
    }else{
    System.out.println(e1.getSQLState() + " " + e1.getErrorCode());
    System.out.println("FATAL ERROR: Database not shutdown " + e1);
    System.exit(0);
    }
    }
    return true;
    }

    public static void closeConnection(Connection conn){
    try{
    conn.close();
    if(print)System.out.println("Connection closed");
    }catch(SQLException e){
    System.out.println("connection NOT closed " + e);
    System.exit(0);
    }
    }









    share|improve this question



























      1












      1








      1







      I created the following three methods but am confused about what shutDownDB() is really doing. Here are my questions:




      1. Why do I need an active connection to shutdown a specific database?

      2. Doesn't it make more sense to close the connection and then shutdown the DB

      3. Why do I need to close a connection if the specific database is shutdown?

      4. How come I'm able to reestablish a connection after the specific db has been shut down?

      5. What is the actual difference between shutting down a specific db and closing the connection?


      NOTE: using derby in embedded mode



          public static Connection openDB(String dbFolderString) {
      Connection conn = null;
      try{
      File dbFolder = new File(dbFolderString);
      String URL = "jdbc:derby:" + dbFolderString + ";create=true";
      if(print)System.out.println("n" + "db exists " + dbFolder.exists());

      conn = DriverManager.getConnection(URL);
      if(print)System.out.println("Succesfully connected to " + dbFolderString);
      }catch(SQLException e){
      System.out.println("FATAL ERROR: from getDB " + e);
      System.exit(0);
      }
      return conn;
      }

      public static boolean shutDownDB(Connection conn) {
      //shutsdown a specific database but DOES NOT SHUTDOWN DERBY
      try{
      String tokens;
      String url = conn.getMetaData().getURL();
      tokens = url.split(":");
      DriverManager.getConnection("jdbc:derby:" + tokens[2] +";shutdown=true");
      }catch(SQLException e1){
      if(e1.getSQLState().equals("08006") && e1.getErrorCode() == 45000){
      if(false)System.out.println(e1.getSQLState() + " " + e1.getErrorCode());
      if(print)System.out.println("n" + "Database shutdown successful");
      }else{
      System.out.println(e1.getSQLState() + " " + e1.getErrorCode());
      System.out.println("FATAL ERROR: Database not shutdown " + e1);
      System.exit(0);
      }
      }
      return true;
      }

      public static void closeConnection(Connection conn){
      try{
      conn.close();
      if(print)System.out.println("Connection closed");
      }catch(SQLException e){
      System.out.println("connection NOT closed " + e);
      System.exit(0);
      }
      }









      share|improve this question















      I created the following three methods but am confused about what shutDownDB() is really doing. Here are my questions:




      1. Why do I need an active connection to shutdown a specific database?

      2. Doesn't it make more sense to close the connection and then shutdown the DB

      3. Why do I need to close a connection if the specific database is shutdown?

      4. How come I'm able to reestablish a connection after the specific db has been shut down?

      5. What is the actual difference between shutting down a specific db and closing the connection?


      NOTE: using derby in embedded mode



          public static Connection openDB(String dbFolderString) {
      Connection conn = null;
      try{
      File dbFolder = new File(dbFolderString);
      String URL = "jdbc:derby:" + dbFolderString + ";create=true";
      if(print)System.out.println("n" + "db exists " + dbFolder.exists());

      conn = DriverManager.getConnection(URL);
      if(print)System.out.println("Succesfully connected to " + dbFolderString);
      }catch(SQLException e){
      System.out.println("FATAL ERROR: from getDB " + e);
      System.exit(0);
      }
      return conn;
      }

      public static boolean shutDownDB(Connection conn) {
      //shutsdown a specific database but DOES NOT SHUTDOWN DERBY
      try{
      String tokens;
      String url = conn.getMetaData().getURL();
      tokens = url.split(":");
      DriverManager.getConnection("jdbc:derby:" + tokens[2] +";shutdown=true");
      }catch(SQLException e1){
      if(e1.getSQLState().equals("08006") && e1.getErrorCode() == 45000){
      if(false)System.out.println(e1.getSQLState() + " " + e1.getErrorCode());
      if(print)System.out.println("n" + "Database shutdown successful");
      }else{
      System.out.println(e1.getSQLState() + " " + e1.getErrorCode());
      System.out.println("FATAL ERROR: Database not shutdown " + e1);
      System.exit(0);
      }
      }
      return true;
      }

      public static void closeConnection(Connection conn){
      try{
      conn.close();
      if(print)System.out.println("Connection closed");
      }catch(SQLException e){
      System.out.println("connection NOT closed " + e);
      System.exit(0);
      }
      }






      java database-connection derby shutdown






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 0:05

























      asked Nov 10 at 23:40









      DCR

      2,36021648




      2,36021648
























          1 Answer
          1






          active

          oldest

          votes


















          2














          Lets start with what the Derby documentation says:




          Shutting down Derby or an individual database



          Applications in an embedded environment shut down the Derby system by
          specifying the shutdown=true attribute in the connection URL. To shut
          down the system, you do not specify a database name, and you do not
          ordinarily specify any other attribute.



          jdbc:derby:;shutdown=true


          A successful shutdown always results in an SQLException to indicate
          that Derby has shut down and that there is no other exception.



          If you have enabled user authentication at the system level, you will
          need to specify credentials (that is, username and password) in order
          to shut down a Derby system, and the supplied username and password
          must also be defined at the system level.




          and so on.





          Your questions:




          1) Why do I need an active connection to shutdown a specific database?





          • Because the documentation says so.


          • Because this is how they have implemented it.


          • Because shutting down the database (in general) requires user
            authentication and connection establishment is the place where user
            authentication happens.




          2) Doesn't it make more sense to close the connection and then shutdown the DB?




          Given the last, no.




          3) Why do I need to close a connection if the specific database is shutdown?




          You don't need to if your application can cope with potential resource leakage; e.g. sockets that may not have been closed on the client side.



          But it is certainly advisable if your application is going to want to continue after shutting down the database.




          4) How come I'm able to reestablish a connection after the specific db has been shut down?




          Presumably, because it is designed to allow that.




          5) What is the actual difference between shutting down a specific db and closing the connection?




          (I'm not entirely sure about this ....)



          Shutting down a database will invalidate all connections for the specific database. It doesn't close them on the client side, so the sockets are liable to remain open .... until the part of your application that uses the respective collections tries to use them, discovers they are "dead" and closes them.



          By contrast, closing a connection closes just that connection, both on the server and client side. Any sockets should be closed immediately.






          NOTE: using derby in embedded mode




          The documentation doesn't make a distinction for embedded and non-embedded mode.






          share|improve this answer





















          • Wonderful answer! One slight improvement is the detail about whether the database will need to perform transaction recovery after a restart. If all connections have been closed, it will not need to undo any transactions, but it may need to redo some transactions. The redo recovery can be avoided if the database itself is cleanly shut down (which is more than just closing all the connections).
            – Bryan Pendleton
            Nov 11 at 15:23













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244504%2fwhat-is-the-proper-way-to-shutdown-a-specific-db-and-close-the-connection%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          Lets start with what the Derby documentation says:




          Shutting down Derby or an individual database



          Applications in an embedded environment shut down the Derby system by
          specifying the shutdown=true attribute in the connection URL. To shut
          down the system, you do not specify a database name, and you do not
          ordinarily specify any other attribute.



          jdbc:derby:;shutdown=true


          A successful shutdown always results in an SQLException to indicate
          that Derby has shut down and that there is no other exception.



          If you have enabled user authentication at the system level, you will
          need to specify credentials (that is, username and password) in order
          to shut down a Derby system, and the supplied username and password
          must also be defined at the system level.




          and so on.





          Your questions:




          1) Why do I need an active connection to shutdown a specific database?





          • Because the documentation says so.


          • Because this is how they have implemented it.


          • Because shutting down the database (in general) requires user
            authentication and connection establishment is the place where user
            authentication happens.




          2) Doesn't it make more sense to close the connection and then shutdown the DB?




          Given the last, no.




          3) Why do I need to close a connection if the specific database is shutdown?




          You don't need to if your application can cope with potential resource leakage; e.g. sockets that may not have been closed on the client side.



          But it is certainly advisable if your application is going to want to continue after shutting down the database.




          4) How come I'm able to reestablish a connection after the specific db has been shut down?




          Presumably, because it is designed to allow that.




          5) What is the actual difference between shutting down a specific db and closing the connection?




          (I'm not entirely sure about this ....)



          Shutting down a database will invalidate all connections for the specific database. It doesn't close them on the client side, so the sockets are liable to remain open .... until the part of your application that uses the respective collections tries to use them, discovers they are "dead" and closes them.



          By contrast, closing a connection closes just that connection, both on the server and client side. Any sockets should be closed immediately.






          NOTE: using derby in embedded mode




          The documentation doesn't make a distinction for embedded and non-embedded mode.






          share|improve this answer





















          • Wonderful answer! One slight improvement is the detail about whether the database will need to perform transaction recovery after a restart. If all connections have been closed, it will not need to undo any transactions, but it may need to redo some transactions. The redo recovery can be avoided if the database itself is cleanly shut down (which is more than just closing all the connections).
            – Bryan Pendleton
            Nov 11 at 15:23


















          2














          Lets start with what the Derby documentation says:




          Shutting down Derby or an individual database



          Applications in an embedded environment shut down the Derby system by
          specifying the shutdown=true attribute in the connection URL. To shut
          down the system, you do not specify a database name, and you do not
          ordinarily specify any other attribute.



          jdbc:derby:;shutdown=true


          A successful shutdown always results in an SQLException to indicate
          that Derby has shut down and that there is no other exception.



          If you have enabled user authentication at the system level, you will
          need to specify credentials (that is, username and password) in order
          to shut down a Derby system, and the supplied username and password
          must also be defined at the system level.




          and so on.





          Your questions:




          1) Why do I need an active connection to shutdown a specific database?





          • Because the documentation says so.


          • Because this is how they have implemented it.


          • Because shutting down the database (in general) requires user
            authentication and connection establishment is the place where user
            authentication happens.




          2) Doesn't it make more sense to close the connection and then shutdown the DB?




          Given the last, no.




          3) Why do I need to close a connection if the specific database is shutdown?




          You don't need to if your application can cope with potential resource leakage; e.g. sockets that may not have been closed on the client side.



          But it is certainly advisable if your application is going to want to continue after shutting down the database.




          4) How come I'm able to reestablish a connection after the specific db has been shut down?




          Presumably, because it is designed to allow that.




          5) What is the actual difference between shutting down a specific db and closing the connection?




          (I'm not entirely sure about this ....)



          Shutting down a database will invalidate all connections for the specific database. It doesn't close them on the client side, so the sockets are liable to remain open .... until the part of your application that uses the respective collections tries to use them, discovers they are "dead" and closes them.



          By contrast, closing a connection closes just that connection, both on the server and client side. Any sockets should be closed immediately.






          NOTE: using derby in embedded mode




          The documentation doesn't make a distinction for embedded and non-embedded mode.






          share|improve this answer





















          • Wonderful answer! One slight improvement is the detail about whether the database will need to perform transaction recovery after a restart. If all connections have been closed, it will not need to undo any transactions, but it may need to redo some transactions. The redo recovery can be avoided if the database itself is cleanly shut down (which is more than just closing all the connections).
            – Bryan Pendleton
            Nov 11 at 15:23
















          2












          2








          2






          Lets start with what the Derby documentation says:




          Shutting down Derby or an individual database



          Applications in an embedded environment shut down the Derby system by
          specifying the shutdown=true attribute in the connection URL. To shut
          down the system, you do not specify a database name, and you do not
          ordinarily specify any other attribute.



          jdbc:derby:;shutdown=true


          A successful shutdown always results in an SQLException to indicate
          that Derby has shut down and that there is no other exception.



          If you have enabled user authentication at the system level, you will
          need to specify credentials (that is, username and password) in order
          to shut down a Derby system, and the supplied username and password
          must also be defined at the system level.




          and so on.





          Your questions:




          1) Why do I need an active connection to shutdown a specific database?





          • Because the documentation says so.


          • Because this is how they have implemented it.


          • Because shutting down the database (in general) requires user
            authentication and connection establishment is the place where user
            authentication happens.




          2) Doesn't it make more sense to close the connection and then shutdown the DB?




          Given the last, no.




          3) Why do I need to close a connection if the specific database is shutdown?




          You don't need to if your application can cope with potential resource leakage; e.g. sockets that may not have been closed on the client side.



          But it is certainly advisable if your application is going to want to continue after shutting down the database.




          4) How come I'm able to reestablish a connection after the specific db has been shut down?




          Presumably, because it is designed to allow that.




          5) What is the actual difference between shutting down a specific db and closing the connection?




          (I'm not entirely sure about this ....)



          Shutting down a database will invalidate all connections for the specific database. It doesn't close them on the client side, so the sockets are liable to remain open .... until the part of your application that uses the respective collections tries to use them, discovers they are "dead" and closes them.



          By contrast, closing a connection closes just that connection, both on the server and client side. Any sockets should be closed immediately.






          NOTE: using derby in embedded mode




          The documentation doesn't make a distinction for embedded and non-embedded mode.






          share|improve this answer












          Lets start with what the Derby documentation says:




          Shutting down Derby or an individual database



          Applications in an embedded environment shut down the Derby system by
          specifying the shutdown=true attribute in the connection URL. To shut
          down the system, you do not specify a database name, and you do not
          ordinarily specify any other attribute.



          jdbc:derby:;shutdown=true


          A successful shutdown always results in an SQLException to indicate
          that Derby has shut down and that there is no other exception.



          If you have enabled user authentication at the system level, you will
          need to specify credentials (that is, username and password) in order
          to shut down a Derby system, and the supplied username and password
          must also be defined at the system level.




          and so on.





          Your questions:




          1) Why do I need an active connection to shutdown a specific database?





          • Because the documentation says so.


          • Because this is how they have implemented it.


          • Because shutting down the database (in general) requires user
            authentication and connection establishment is the place where user
            authentication happens.




          2) Doesn't it make more sense to close the connection and then shutdown the DB?




          Given the last, no.




          3) Why do I need to close a connection if the specific database is shutdown?




          You don't need to if your application can cope with potential resource leakage; e.g. sockets that may not have been closed on the client side.



          But it is certainly advisable if your application is going to want to continue after shutting down the database.




          4) How come I'm able to reestablish a connection after the specific db has been shut down?




          Presumably, because it is designed to allow that.




          5) What is the actual difference between shutting down a specific db and closing the connection?




          (I'm not entirely sure about this ....)



          Shutting down a database will invalidate all connections for the specific database. It doesn't close them on the client side, so the sockets are liable to remain open .... until the part of your application that uses the respective collections tries to use them, discovers they are "dead" and closes them.



          By contrast, closing a connection closes just that connection, both on the server and client side. Any sockets should be closed immediately.






          NOTE: using derby in embedded mode




          The documentation doesn't make a distinction for embedded and non-embedded mode.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 6:52









          Stephen C

          512k69561912




          512k69561912












          • Wonderful answer! One slight improvement is the detail about whether the database will need to perform transaction recovery after a restart. If all connections have been closed, it will not need to undo any transactions, but it may need to redo some transactions. The redo recovery can be avoided if the database itself is cleanly shut down (which is more than just closing all the connections).
            – Bryan Pendleton
            Nov 11 at 15:23




















          • Wonderful answer! One slight improvement is the detail about whether the database will need to perform transaction recovery after a restart. If all connections have been closed, it will not need to undo any transactions, but it may need to redo some transactions. The redo recovery can be avoided if the database itself is cleanly shut down (which is more than just closing all the connections).
            – Bryan Pendleton
            Nov 11 at 15:23


















          Wonderful answer! One slight improvement is the detail about whether the database will need to perform transaction recovery after a restart. If all connections have been closed, it will not need to undo any transactions, but it may need to redo some transactions. The redo recovery can be avoided if the database itself is cleanly shut down (which is more than just closing all the connections).
          – Bryan Pendleton
          Nov 11 at 15:23






          Wonderful answer! One slight improvement is the detail about whether the database will need to perform transaction recovery after a restart. If all connections have been closed, it will not need to undo any transactions, but it may need to redo some transactions. The redo recovery can be avoided if the database itself is cleanly shut down (which is more than just closing all the connections).
          – Bryan Pendleton
          Nov 11 at 15:23




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244504%2fwhat-is-the-proper-way-to-shutdown-a-specific-db-and-close-the-connection%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()