提交 23959cee authored 作者: Thomas Mueller's avatar Thomas Mueller

Simplify SessionFactory a bit.

上级 b557fb3f
...@@ -24,15 +24,12 @@ import org.h2.util.StringUtils; ...@@ -24,15 +24,12 @@ import org.h2.util.StringUtils;
* It is also responsible for opening and creating new databases. * It is also responsible for opening and creating new databases.
* This is a singleton class. * This is a singleton class.
*/ */
public class Engine { public class Engine implements SessionFactory {
private static final Engine INSTANCE = new Engine(); private static final Engine INSTANCE = new Engine();
private static final HashMap<String, Database> DATABASES = New.hashMap();
private volatile long wrongPasswordDelay = SysProperties.DELAY_WRONG_PASSWORD_MIN;
private Engine() { private final HashMap<String, Database> databases = New.hashMap();
// don't allow others to instantiate private volatile long wrongPasswordDelay = SysProperties.DELAY_WRONG_PASSWORD_MIN;
}
public static Engine getInstance() { public static Engine getInstance() {
return INSTANCE; return INSTANCE;
...@@ -45,7 +42,7 @@ public class Engine { ...@@ -45,7 +42,7 @@ public class Engine {
if (openNew || ci.isUnnamedInMemory()) { if (openNew || ci.isUnnamedInMemory()) {
database = null; database = null;
} else { } else {
database = DATABASES.get(name); database = databases.get(name);
} }
User user = null; User user = null;
boolean opened = false; boolean opened = false;
...@@ -64,7 +61,7 @@ public class Engine { ...@@ -64,7 +61,7 @@ public class Engine {
database.setMasterUser(user); database.setMasterUser(user);
} }
if (!ci.isUnnamedInMemory()) { if (!ci.isUnnamedInMemory()) {
DATABASES.put(name, database); databases.put(name, database);
} }
} }
synchronized (database) { synchronized (database) {
...@@ -108,7 +105,11 @@ public class Engine { ...@@ -108,7 +105,11 @@ public class Engine {
* @param ci the connection information * @param ci the connection information
* @return the session * @return the session
*/ */
public Session getSession(ConnectionInfo ci) { public Session createSession(ConnectionInfo ci) {
return INSTANCE.createSessionAndValidate(ci);
}
private Session createSessionAndValidate(ConnectionInfo ci) {
try { try {
ConnectionInfo backup = null; ConnectionInfo backup = null;
String lockMethodName = ci.getProperty("FILE_LOCK", null); String lockMethodName = ci.getProperty("FILE_LOCK", null);
...@@ -213,7 +214,7 @@ public class Engine { ...@@ -213,7 +214,7 @@ public class Engine {
* @param name the database name * @param name the database name
*/ */
public void close(String name) { public void close(String name) {
DATABASES.remove(name); databases.remove(name);
} }
/** /**
......
...@@ -42,7 +42,7 @@ import org.h2.value.ValueString; ...@@ -42,7 +42,7 @@ import org.h2.value.ValueString;
* mode, this object resides on the server side and communicates with a * mode, this object resides on the server side and communicates with a
* SessionRemote object on the client side. * SessionRemote object on the client side.
*/ */
public class Session extends SessionWithState implements SessionFactory { public class Session extends SessionWithState {
/** /**
* This special log position means that the log entry has been written. * This special log position means that the log entry has been written.
...@@ -118,10 +118,6 @@ public class Session extends SessionWithState implements SessionFactory { ...@@ -118,10 +118,6 @@ public class Session extends SessionWithState implements SessionFactory {
this.currentSchemaName = Constants.SCHEMA_MAIN; this.currentSchemaName = Constants.SCHEMA_MAIN;
} }
public SessionInterface createSession(ConnectionInfo ci) {
return Engine.getInstance().getSession(ci);
}
public boolean setCommitOrRollbackDisabled(boolean x) { public boolean setCommitOrRollbackDisabled(boolean x) {
boolean old = commitOrRollbackDisabled; boolean old = commitOrRollbackDisabled;
commitOrRollbackDisabled = x; commitOrRollbackDisabled = x;
...@@ -1178,7 +1174,7 @@ public class Session extends SessionWithState implements SessionFactory { ...@@ -1178,7 +1174,7 @@ public class Session extends SessionWithState implements SessionFactory {
public SessionInterface reconnect(boolean write) { public SessionInterface reconnect(boolean write) {
readSessionState(); readSessionState();
close(); close();
Session newSession = Engine.getInstance().getSession(connectionInfo); Session newSession = Engine.getInstance().createSession(connectionInfo);
newSession.sessionState = sessionState; newSession.sessionState = sessionState;
newSession.recreateSessionState(); newSession.recreateSessionState();
if (write) { if (write) {
......
...@@ -108,7 +108,7 @@ public class JdbcConnection extends TraceObject implements Connection { ...@@ -108,7 +108,7 @@ public class JdbcConnection extends TraceObject implements Connection {
} }
checkJavaVersion(); checkJavaVersion();
// this will return an embedded or server connection // this will return an embedded or server connection
session = new SessionRemote().createSession(ci); session = new SessionRemote(ci).createSession(ci);
trace = session.getTrace(); trace = session.getTrace();
int id = getNextId(TraceObject.CONNECTION); int id = getNextId(TraceObject.CONNECTION);
setTrace(trace, TraceObject.CONNECTION, id); setTrace(trace, TraceObject.CONNECTION, id);
......
...@@ -121,8 +121,7 @@ public class TcpServerThread implements Runnable { ...@@ -121,8 +121,7 @@ public class TcpServerThread implements Runnable {
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
ci.setProperty(transfer.readString(), transfer.readString()); ci.setProperty(transfer.readString(), transfer.readString());
} }
Engine engine = Engine.getInstance(); session = Engine.getInstance().createSession(ci);
session = engine.getSession(ci);
transfer.setSession(session); transfer.setSession(session);
transfer.writeInt(SessionRemote.STATUS_OK); transfer.writeInt(SessionRemote.STATUS_OK);
transfer.writeInt(clientVersion); transfer.writeInt(clientVersion);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论