Quantcast
Channel: MySQL Forums - Connector/J, JDBC and Java
Viewing all articles
Browse latest Browse all 884

Switching database connection in mysql (2 replies)

$
0
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

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

Viewing all articles
Browse latest Browse all 884

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>