title: Switching database connection in mysql
body: <p>Here my problem :
  I have two mysql databases directory and I want to use one after the other.</p>
  
  <p>The only way that I have actualy found, to switch from one database to the other, is to shutdown the mysql daemon and to start it again pointing to the second database directory.</p>
  
  <p>Are there any other way to perform that ?</p>
  
  <p>Thanks</p>
  
  <hr>
  
  <p><strong>EDIT :</strong>
  My application manage "missions directory" that embed a Database.</p>
  
  <p>This missions are copied to an hard disk, that is connected to an external device that will fill this database.</p>
  
  <p>Then when the mission is done, we collect the mission and the database with the application to generate report.</p>
  
  <p>That why we have multiple database with the same schema, but placed in different place, we need also to read this database by an external application that why we need to have only one database open at each time.</p>
  
  <p>My question is <strong><em>not</em></strong> if it possible to <strong><em>run two database from two different directories at the same time</em></strong>, because I know that is possible, but <strong><em>how to switch from one database to another</em></strong>, without kiling the daemon.</p>
  
  <hr>
  
  <p>PS: I'm working on Java application and I do all this action by system access in Java like <code>Runtime.getRuntime().exec(MY_CMD)</code>, not by choice. Maybe it's better to use Java Library, I already use hibernate.</p>
  
  <p>Here the code to switch :</p>
  
  <pre><code>        new Thread(new Task&lt;T&gt;() {
          @Override
          protected T call() throws Exception {
  
              // Close the previous database
              if (isDaemonRunning()) {
                  close();
              }
  
              // try to open the new one
              if (!open()) {
                  notifyConnectedStatus(false);
                  return null;
              }
  
              // create the hibernate session object
              _session = HibernateUtil.getSessionFactory().openSession();
  
              notifyConnectedStatus(true);
  
              // no return is waiting, then return null
              return null;
          }
  
      }).start();
  </code></pre>
  
  <p>Here the called methods :</p>
  
  <pre><code>private boolean open() {
      int exitVal = 0;
      try {
          Process p = Runtime.getRuntime().exec(getRunDaemonCmd());
          p.waitFor(1, TimeUnit.SECONDS);
          if (p.isAlive()) {
              return true;
          }
          exitVal = p.exitValue();
      } catch (Exception e) {
          _logger.log(Level.SEVERE, e.getMessage(), e);
          return false;
      }
      return (0 == exitVal);
  }
  
  private void close() {
      do {
          try {
              if (null != _session) {
                  _session.close();
                  _session = null;
              }
  
              Process p = Runtime.getRuntime().exec(SHUTDOWN_CMD);
              p.waitFor();
          } catch (Exception e) {
              _logger.log(Level.SEVERE, e.getMessage(), e);
              return;
          }
      } while (isDaemonRunning());
      _connected = false;
  }
  
  
  private String[] getRunDaemonCmd() {
      return new String[] { MYSQLD, INI_FILE_PARAM + _myIniFile, DATADIR_PARAM + _databasePath };
  }
  
  private boolean isDaemonRunning() {
      int exitVal = 0;
      try {
          Process p = Runtime.getRuntime().exec(PING_CMD);
          p.waitFor();
          exitVal = p.exitValue();
      } catch (Exception e) {
          _logger.log(Level.SEVERE, e.getMessage(), e);
      }
      return (0 == exitVal);
  }
  </code></pre>
  
  <p>And Here the constants :</p>
  
  <pre><code>private static final String MYSQLD = "mysqld";
  private static final String INI_FILE_PARAM = "--defaults-file=";
  private static final String DATADIR_PARAM = "--datadir=";
  
  private static final String MYSQLADMIN = "mysqladmin";
  
  private static final String USER_PARAM = "-u";
  private static final String PASSWORD_PARAM = "-p";
  private static final String SHUTDOWN = "shutdown";
  
  private static final String PING = "ping";
  
  private static final String[] PING_CMD = new String[] { MYSQLADMIN, PING };
  
  private static final String[] SHUTDOWN_CMD = new String[] { MYSQLADMIN, USER_PARAM + DatabaseSettings.getUser(),
          PASSWORD_PARAM + DatabaseSettings.getPassword(), SHUTDOWN };
  
  private String _myIniFile = DatabaseSettings.getDefaultIniFile();
  </code></pre>