0

Here my problem : I have two mysql databases directory and I want to use one after the other.

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.

Are there any other way to perform that ?

Thanks


EDIT : My application manage "missions directory" that embed a Database.

This missions are copied to an hard disk, that is connected to an external device that will fill this database.

Then when the mission is done, we collect the mission and the database with the application to generate report.

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.

My question is not if it possible to run two database from two different directories at the same time, because I know that is possible, but how to switch from one database to another, without kiling the daemon.


PS: I'm working on Java application and I do all this action by system access in Java like Runtime.getRuntime().exec(MY_CMD), not by choice. Maybe it's better to use Java Library, I already use hibernate.

Here the code to switch :

        new Thread(new Task<T>() {
        @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();

Here the called methods :

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);
}

And Here the constants :

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();
MonkeyJLuffy
  • 195
  • 3
  • 15
  • 1
    This is wrong in every way. It can never, ever scale. You need to have two databases running all the time. If they are on the same server I'd suggest starting them to listen on different ports. – duffymo Apr 19 '17 at 09:12
  • I don't need to have two datadabses running at the same time and I cannot. You need to understand that my application copy directories from one space to other. That directories contains a different database. I need to switch a running time to any database that I want. By the way, That I have doing wok well, but I want to know if ther are a better way. – MonkeyJLuffy Apr 19 '17 at 09:28

1 Answers1

0

so, you can use multiple persistence unit to connect with multiple data-source or database if you use hibernate.

Subrata nath
  • 172
  • 10
  • The fact is I don't know before running the daemon, where are the databases and how many I have, like I explain before, we copy directories that contains the databases, so the path change all the time – MonkeyJLuffy Apr 19 '17 at 09:30
  • I added more details in the description of my problem – MonkeyJLuffy Apr 20 '17 at 07:41