提交 ad601a53 authored 作者: Thomas Mueller's avatar Thomas Mueller

The database upgrade (to upgrade from H2 version 1.1.x) has been simplified.

上级 32b28f61
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
*/ */
package org.h2; package org.h2;
import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.DriverPropertyInfo; import java.sql.DriverPropertyInfo;
import java.sql.SQLException; import java.sql.SQLException;
...@@ -46,7 +47,7 @@ public class Driver implements java.sql.Driver { ...@@ -46,7 +47,7 @@ public class Driver implements java.sql.Driver {
* @param info the connection properties * @param info the connection properties
* @return the new connection or null if the URL is not supported * @return the new connection or null if the URL is not supported
*/ */
public JdbcConnection connect(String url, Properties info) throws SQLException { public Connection connect(String url, Properties info) throws SQLException {
try { try {
if (info == null) { if (info == null) {
info = new Properties(); info = new Properties();
...@@ -54,7 +55,10 @@ public class Driver implements java.sql.Driver { ...@@ -54,7 +55,10 @@ public class Driver implements java.sql.Driver {
if (!acceptsURL(url)) { if (!acceptsURL(url)) {
return null; return null;
} }
DbUpgrade.upgradeIfRequired(url, info); Connection c = DbUpgrade.connctOrUpgrade(url, info);
if (c != null) {
return c;
}
return new JdbcConnection(url, info); return new JdbcConnection(url, info);
} catch (Exception e) { } catch (Exception e) {
throw DbException.toSQLException(e); throw DbException.toSQLException(e);
......
...@@ -83,7 +83,7 @@ public class ConnectionInfo implements Cloneable { ...@@ -83,7 +83,7 @@ public class ConnectionInfo implements Cloneable {
set.addAll(list); set.addAll(list);
String[] connectionTime = { "ACCESS_MODE_DATA", "AUTOCOMMIT", "CIPHER", String[] connectionTime = { "ACCESS_MODE_DATA", "AUTOCOMMIT", "CIPHER",
"CREATE", "CACHE_TYPE", "FILE_LOCK", "IGNORE_UNKNOWN_SETTINGS", "IFEXISTS", "CREATE", "CACHE_TYPE", "FILE_LOCK", "IGNORE_UNKNOWN_SETTINGS", "IFEXISTS",
"INIT", "PASSWORD", "RECOVER", "USER", "AUTO_SERVER", "INIT", "PASSWORD", "RECOVER", "USER", "AUTO_SERVER", "NO_UPGRADE",
"AUTO_RECONNECT", "OPEN_NEW", "PAGE_SIZE", "PASSWORD_HASH", "JMX" }; "AUTO_RECONNECT", "OPEN_NEW", "PAGE_SIZE", "PASSWORD_HASH", "JMX" };
for (String key : connectionTime) { for (String key : connectionTime) {
if (SysProperties.CHECK && set.contains(key)) { if (SysProperties.CHECK && set.contains(key)) {
......
...@@ -40,6 +40,7 @@ public class Engine implements SessionFactory { ...@@ -40,6 +40,7 @@ public class Engine implements SessionFactory {
private Session openSession(ConnectionInfo ci, boolean ifExists, String cipher) { private Session openSession(ConnectionInfo ci, boolean ifExists, String cipher) {
String name = ci.getName(); String name = ci.getName();
Database database; Database database;
ci.removeProperty("NO_UPGRADE", false);
boolean openNew = ci.getProperty("OPEN_NEW", false); boolean openNew = ci.getProperty("OPEN_NEW", false);
if (openNew || ci.isUnnamedInMemory()) { if (openNew || ci.isUnnamedInMemory()) {
database = null; database = null;
......
...@@ -177,11 +177,13 @@ implements XADataSource, DataSource, ConnectionPoolDataSource, Serializable, Ref ...@@ -177,11 +177,13 @@ implements XADataSource, DataSource, ConnectionPoolDataSource, Serializable, Ref
Properties info = new Properties(); Properties info = new Properties();
info.setProperty("user", user); info.setProperty("user", user);
info.put("password", password); info.put("password", password);
JdbcConnection conn = Driver.load().connect(url, info); Connection conn = Driver.load().connect(url, info);
if (conn == null) { if (conn == null) {
throw new SQLException("No suitable driver found for " + url, "08001", 8001); throw new SQLException("No suitable driver found for " + url, "08001", 8001);
} else if (!(conn instanceof JdbcConnection)) {
throw new SQLException("Connecting with old version is not supported: " + url, "08001", 8001);
} }
return conn; return (JdbcConnection) conn;
} }
/** /**
......
...@@ -45,9 +45,9 @@ public class DbUpgrade { ...@@ -45,9 +45,9 @@ public class DbUpgrade {
* @param url the database URL * @param url the database URL
* @param info the properties * @param info the properties
*/ */
public static void upgradeIfRequired(String url, Properties info) throws SQLException { public static Connection connctOrUpgrade(String url, Properties info) throws SQLException {
if (!upgradeClassesPresent) { if (!upgradeClassesPresent) {
return; return null;
} }
Properties i2 = new Properties(); Properties i2 = new Properties();
i2.putAll(info); i2.putAll(info);
...@@ -59,18 +59,21 @@ public class DbUpgrade { ...@@ -59,18 +59,21 @@ public class DbUpgrade {
info = i2; info = i2;
ConnectionInfo ci = new ConnectionInfo(url, info); ConnectionInfo ci = new ConnectionInfo(url, info);
if (ci.isRemote() || !ci.isPersistent()) { if (ci.isRemote() || !ci.isPersistent()) {
return; return null;
} }
String name = ci.getName(); String name = ci.getName();
if (Database.exists(name)) { if (Database.exists(name)) {
return; return null;
} }
if (!IOUtils.exists(name + ".data.db")) { if (!IOUtils.exists(name + ".data.db")) {
return; return null;
}
if (ci.removeProperty("NO_UPGRADE", false)) {
return connectWithOldVersion(url, info);
} }
synchronized (DbUpgrade.class) { synchronized (DbUpgrade.class) {
upgrade(ci, info); upgrade(ci, info);
return; return null;
} }
} }
...@@ -97,6 +100,11 @@ public class DbUpgrade { ...@@ -97,6 +100,11 @@ public class DbUpgrade {
DbUpgrade.deleteOldDb = deleteOldDb; DbUpgrade.deleteOldDb = deleteOldDb;
} }
private static Connection connectWithOldVersion(String url, Properties info) throws SQLException {
url = "jdbc:h2v1_1:" + url.substring("jdbc:h2:".length()) + ";IGNORE_UNKNOWN_SETTINGS=TRUE";
return DriverManager.getConnection(url, info);
}
private static void upgrade(ConnectionInfo ci, Properties info) throws SQLException { private static void upgrade(ConnectionInfo ci, Properties info) throws SQLException {
String name = ci.getName(); String name = ci.getName();
String data = name + ".data.db"; String data = name + ".data.db";
......
...@@ -93,10 +93,7 @@ public class TestUpgrade extends TestBase { ...@@ -93,10 +93,7 @@ public class TestUpgrade extends TestBase {
assertTrue(IOUtils.exists(getBaseDir() + "/upgrade.h2.db")); assertTrue(IOUtils.exists(getBaseDir() + "/upgrade.h2.db"));
deleteDb("upgrade"); deleteDb("upgrade");
conn = getConnection("jdbc:h2v1_1:" + getBaseDir() + "/upgrade"); conn = getConnection("upgrade;NO_UPGRADE=TRUE");
conn.close();
conn = getConnection("upgrade");
conn.close(); conn.close();
assertTrue(IOUtils.exists(getBaseDir() + "/upgrade.h2.db")); assertTrue(IOUtils.exists(getBaseDir() + "/upgrade.h2.db"));
deleteDb("upgrade"); deleteDb("upgrade");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论