提交 7f1e5589 authored 作者: christian.peter.io's avatar christian.peter.io

If an old 1.1 database file version is found on connect, it is now possible to let the old h2

  classes (v 1.2.128) connect to the database. The automatic upgrade .jar file must be present, and the url
  must contain NO_UPGRADE=TRUE
上级 d46b6c73
......@@ -1261,6 +1261,10 @@ Both defaults can be customized via
</li></ul>
prior opening a database connection.
</p>
<p>
Since version 1.2.140 it is possible to let the old h2 classes (v 1.2.128) connect to the database. The automatic upgrade .jar file must be present, and the url
must contain NO_UPGRADE=TRUE.
</p>
<h2 id="limits_limitations">Limits and Limitations</h2>
<p>
......
......@@ -25,6 +25,9 @@ Change Log
because it was not always working as expected. Now System.gc() is used in a loop until the
buffer is garbage collected. This of course is also not a nice solution, but the only one known to work.
See also http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4724038
</li><li>If an old 1.1 database file version is found on connect, it is now possible to let the old h2
classes (v 1.2.128) connect to the database. The automatic upgrade .jar file must be present, and the url
must contain NO_UPGRADE=TRUE
</li></ul>
<h2>Version 1.2.139 (2010-07-10)</h2>
......
......@@ -56,8 +56,18 @@ public class Driver implements java.sql.Driver {
if (!acceptsURL(url)) {
return null;
}
// Upgrade the database if needed and conversion classes are found
DbUpgrade.upgrade(url, info);
boolean noUpgrade = url.contains(";NO_UPGRADE=TRUE");
url = url.replaceAll(";NO_UPGRADE=TRUE", "");
if (DbUpgrade.areV1_1ClassesPresent()) {
if (noUpgrade) {
Connection connection = DbUpgrade.connectWithOldVersion(url, info);
if (connection != null) {
return connection;
}
} else {
DbUpgrade.upgrade(url, info);
}
}
return new JdbcConnection(url, info);
} catch (Exception e) {
......
......@@ -6,11 +6,18 @@
*/
package org.h2.upgrade;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Collections;
import java.util.Hashtable;
import java.util.Map;
import java.util.Properties;
import org.h2.jdbc.JdbcConnection;
import org.h2.message.DbException;
import org.h2.store.fs.FileSystem;
import org.h2.store.fs.FileSystemDisk;
import org.h2.util.Utils;
/**
......@@ -19,26 +26,74 @@ import org.h2.util.Utils;
*/
public class DbUpgrade {
private static boolean nonPageStoreToCurrentEnabled;
private static boolean v1_1ClassesPresent;
private static Map<String, DbUpgradeNonPageStoreToCurrent> runningConversions;
static {
// static initialize block
nonPageStoreToCurrentEnabled = Utils.isClassPresent("org.h2.upgrade.v1_1.Driver");
v1_1ClassesPresent = Utils.isClassPresent("org.h2.upgrade.v1_1.Driver");
runningConversions = Collections.synchronizedMap(new Hashtable<String, DbUpgradeNonPageStoreToCurrent>(1));
}
public static boolean areV1_1ClassesPresent() {
return v1_1ClassesPresent;
}
/**
* Connects to an old 1.1 database
*
* @param url The connection string
* @param info The connection properties
* @return the connection via the upgrade classes
* @throws SQLException
*/
public static Connection connectWithOldVersion(String url, Properties info) throws SQLException {
try {
String oldStartUrlPrefix = (String) Utils.getStaticField("org.h2.upgrade.v1_1.engine.Constants.START_URL");
url = url.replaceAll(org.h2.engine.Constants.START_URL, oldStartUrlPrefix);
url = url.replaceAll(";IGNORE_UNKNOWN_SETTINGS=TRUE", "");
url = url.replaceAll(";IGNORE_UNKNOWN_SETTINGS=FALSE", "");
url = url.replaceAll(";PAGE_STORE=TRUE", "");
url += ";IGNORE_UNKNOWN_SETTINGS=TRUE";
Object ci = Utils.newInstance("org.h2.upgrade.v1_1.engine.ConnectionInfo", url, info);
boolean isRemote = (Boolean) Utils.callMethod(ci, "isRemote");
boolean isPersistent = (Boolean) Utils.callMethod(ci, "isPersistent");
String dbName = (String) Utils.callMethod(ci, "getName");
// remove stackable file systems
int colon = dbName.indexOf(':');
while (colon != -1) {
String fileSystemPrefix = dbName.substring(0, colon+1);
FileSystem fs = FileSystem.getInstance(fileSystemPrefix);
if (fs == null || fs instanceof FileSystemDisk) {
break;
}
dbName = dbName.substring(colon+1);
colon = dbName.indexOf(':');
}
File oldDataFile = new File(dbName + ".data.db");
if (!isRemote && isPersistent && oldDataFile.exists()) {
Utils.callStaticMethod("org.h2.upgrade.v1_1.Driver.load");
Connection connection = DriverManager.getConnection(url, info);
return connection;
} else {
return null;
}
} catch (Exception e) {
throw DbException.toSQLException(e);
}
}
/**
* Starts the conversion if the respective classes are found. Is automatically
* called on connect.
*
* @param url The connection string
* @param info The connection Properties
* @param info The connection properties
* @throws SQLException
*/
public static synchronized void upgrade(String url, Properties info) throws SQLException {
if (nonPageStoreToCurrentEnabled) {
if (v1_1ClassesPresent) {
upgradeFromNonPageStore(url, info);
}
}
......
......@@ -69,6 +69,7 @@ public class DbUpgradeNonPageStoreToCurrent {
oldUrl = oldUrl.replaceAll(";IGNORE_UNKNOWN_SETTINGS=FALSE", "");
oldUrl = oldUrl.replaceAll(";IFEXISTS=TRUE", "");
oldUrl = oldUrl.replaceAll(";IFEXISTS=FALSE", "");
oldUrl = oldUrl.replaceAll(";PAGE_STORE=TRUE", "");
oldUrl += ";IGNORE_UNKNOWN_SETTINGS=TRUE";
Object ci = Utils.newInstance("org.h2.upgrade.v1_1.engine.ConnectionInfo", oldUrl, info);
boolean isRemote = (Boolean) Utils.callMethod(ci, "isRemote");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论