Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEike Stepper2013-03-03 13:41:03 -0500
committerEike Stepper2013-03-07 08:44:07 -0500
commit2174001afb162668a7ccaca8aaae293ff7f9eb24 (patch)
tree37c79030f18c897e4b61ac1b9ef66b4f71fc1997
parentc6e79fb5a2912ff61377afeda3235c6a5ff6084f (diff)
downloadcdo-2174001afb162668a7ccaca8aaae293ff7f9eb24.tar.gz
cdo-2174001afb162668a7ccaca8aaae293ff7f9eb24.tar.xz
cdo-2174001afb162668a7ccaca8aaae293ff7f9eb24.zip
[401763] Make CDO Server more robust against data dictionary changes
https://bugs.eclipse.org/bugs/show_bug.cgi?id=401763
-rw-r--r--plugins/org.eclipse.net4j.db.tests/src/org/eclipse/net4j/db/tests/AbstractDBTest.java82
-rw-r--r--plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBUtil.java13
-rw-r--r--plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IDBDatabase.java (renamed from plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IDBInstance.java)16
-rw-r--r--plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IDBPreparedStatement.java2
-rw-r--r--plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IDBSchemaTransaction.java10
-rw-r--r--plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IDBTransaction.java (renamed from plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IDBConnection.java)8
-rw-r--r--plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/ddl/IDBSchemaProvider.java20
-rw-r--r--plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBDatabase.java145
-rw-r--r--plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBInstance.java140
-rw-r--r--plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBPreparedStatement.java12
-rw-r--r--plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBSchemaTransaction.java93
-rw-r--r--plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBTransaction.java (renamed from plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBConnection.java)30
12 files changed, 296 insertions, 275 deletions
diff --git a/plugins/org.eclipse.net4j.db.tests/src/org/eclipse/net4j/db/tests/AbstractDBTest.java b/plugins/org.eclipse.net4j.db.tests/src/org/eclipse/net4j/db/tests/AbstractDBTest.java
index af98636a14..d37695fb3b 100644
--- a/plugins/org.eclipse.net4j.db.tests/src/org/eclipse/net4j/db/tests/AbstractDBTest.java
+++ b/plugins/org.eclipse.net4j.db.tests/src/org/eclipse/net4j/db/tests/AbstractDBTest.java
@@ -13,9 +13,8 @@ package org.eclipse.net4j.db.tests;
import org.eclipse.net4j.db.DBType;
import org.eclipse.net4j.db.DBUtil;
import org.eclipse.net4j.db.IDBAdapter;
-import org.eclipse.net4j.db.IDBConnection;
import org.eclipse.net4j.db.IDBConnectionProvider;
-import org.eclipse.net4j.db.IDBInstance;
+import org.eclipse.net4j.db.IDBDatabase;
import org.eclipse.net4j.db.IDBSchemaTransaction;
import org.eclipse.net4j.db.ddl.IDBField;
import org.eclipse.net4j.db.ddl.IDBIndex;
@@ -122,17 +121,16 @@ public abstract class AbstractDBTest extends AbstractOMTest
public void testSchemaAddition() throws Exception
{
- IDBInstance dbInstance = DBUtil.createInstance(dbAdapter, dbConnectionProvider, "test");
- assertEquals(true, dbInstance.getDBSchema().isLocked());
- assertEquals(true, dbInstance.getDBSchema().isEmpty());
+ IDBDatabase database = DBUtil.createDatabase(dbAdapter, dbConnectionProvider, "test");
+ assertEquals(true, database.getSchema().isLocked());
+ assertEquals(true, database.getSchema().isEmpty());
- IDBConnection dbConnection = dbInstance.openDBConnection();
- IDBSchemaTransaction transaction = dbConnection.startSchemaTransaction();
- assertEquals(false, transaction.getDBSchema().isLocked());
- assertEquals(true, transaction.getDBSchema().isEmpty());
- assertEquals(dbInstance.getDBSchema().getName(), transaction.getDBSchema().getName());
+ IDBSchemaTransaction schemaTransaction = database.openSchemaTransaction();
+ assertEquals(false, schemaTransaction.getSchema().isLocked());
+ assertEquals(true, schemaTransaction.getSchema().isEmpty());
+ assertEquals(database.getSchema().getName(), schemaTransaction.getSchema().getName());
- IDBTable table1 = transaction.getDBSchema().addTable("table1");
+ IDBTable table1 = schemaTransaction.getSchema().addTable("table1");
IDBField field11 = table1.addField("field1", DBType.INTEGER);
IDBField field12 = table1.addField("field2", DBType.VARCHAR, 64);
IDBField field13 = table1.addField("field3", DBType.BOOLEAN);
@@ -140,7 +138,7 @@ public abstract class AbstractDBTest extends AbstractOMTest
IDBIndex index12 = table1.addIndex("index2", IDBIndex.Type.UNIQUE, field11, field12);
IDBIndex index13 = table1.addIndex("index3", IDBIndex.Type.NON_UNIQUE, field12);
- IDBTable table2 = transaction.getDBSchema().addTable("table2");
+ IDBTable table2 = schemaTransaction.getSchema().addTable("table2");
IDBField field21 = table2.addField("field1", DBType.INTEGER);
IDBField field22 = table2.addField("field2", DBType.VARCHAR, 64);
IDBField field23 = table2.addField("field3", DBType.BOOLEAN);
@@ -148,35 +146,35 @@ public abstract class AbstractDBTest extends AbstractOMTest
IDBIndex index22 = table2.addIndex("index2", IDBIndex.Type.UNIQUE, field21, field22);
IDBIndex index23 = table2.addIndex("index3", IDBIndex.Type.NON_UNIQUE, field22);
- transaction.commit();
- assertEquals(true, dbInstance.getDBSchema().isLocked());
- assertEquals(false, dbInstance.getDBSchema().isEmpty());
- assertEquals(2, dbInstance.getDBSchema().getTables().length);
-
- assertEquals(table1.getName(), dbInstance.getDBSchema().getTables()[0].getName());
- assertEquals(table1.getFieldCount(), dbInstance.getDBSchema().getTables()[0].getFieldCount());
- assertEquals(field11.getName(), dbInstance.getDBSchema().getTables()[0].getField(0).getName());
- assertEquals(field12.getName(), dbInstance.getDBSchema().getTables()[0].getField(1).getName());
- assertEquals(field13.getName(), dbInstance.getDBSchema().getTables()[0].getField(2).getName());
- assertEquals(table1.getIndexCount(), dbInstance.getDBSchema().getTables()[0].getIndexCount());
- assertEquals(index11.getName(), dbInstance.getDBSchema().getTables()[0].getIndex(0).getName());
- assertEquals(index11.getType(), dbInstance.getDBSchema().getTables()[0].getIndex(0).getType());
- assertEquals(index12.getName(), dbInstance.getDBSchema().getTables()[0].getIndex(1).getName());
- assertEquals(index12.getType(), dbInstance.getDBSchema().getTables()[0].getIndex(1).getType());
- assertEquals(index13.getName(), dbInstance.getDBSchema().getTables()[0].getIndex(2).getName());
- assertEquals(index13.getType(), dbInstance.getDBSchema().getTables()[0].getIndex(2).getType());
-
- assertEquals(table2.getName(), dbInstance.getDBSchema().getTables()[1].getName());
- assertEquals(table2.getFieldCount(), dbInstance.getDBSchema().getTables()[1].getFieldCount());
- assertEquals(field21.getName(), dbInstance.getDBSchema().getTables()[1].getField(0).getName());
- assertEquals(field22.getName(), dbInstance.getDBSchema().getTables()[1].getField(1).getName());
- assertEquals(field23.getName(), dbInstance.getDBSchema().getTables()[1].getField(2).getName());
- assertEquals(table2.getIndexCount(), dbInstance.getDBSchema().getTables()[1].getIndexCount());
- assertEquals(index21.getName(), dbInstance.getDBSchema().getTables()[1].getIndex(0).getName());
- assertEquals(index21.getType(), dbInstance.getDBSchema().getTables()[1].getIndex(0).getType());
- assertEquals(index22.getName(), dbInstance.getDBSchema().getTables()[1].getIndex(1).getName());
- assertEquals(index22.getType(), dbInstance.getDBSchema().getTables()[1].getIndex(1).getType());
- assertEquals(index23.getName(), dbInstance.getDBSchema().getTables()[1].getIndex(2).getName());
- assertEquals(index23.getType(), dbInstance.getDBSchema().getTables()[1].getIndex(2).getType());
+ schemaTransaction.commit();
+ assertEquals(true, database.getSchema().isLocked());
+ assertEquals(false, database.getSchema().isEmpty());
+ assertEquals(2, database.getSchema().getTables().length);
+
+ assertEquals(table1.getName(), database.getSchema().getTables()[0].getName());
+ assertEquals(table1.getFieldCount(), database.getSchema().getTables()[0].getFieldCount());
+ assertEquals(field11.getName(), database.getSchema().getTables()[0].getField(0).getName());
+ assertEquals(field12.getName(), database.getSchema().getTables()[0].getField(1).getName());
+ assertEquals(field13.getName(), database.getSchema().getTables()[0].getField(2).getName());
+ assertEquals(table1.getIndexCount(), database.getSchema().getTables()[0].getIndexCount());
+ assertEquals(index11.getName(), database.getSchema().getTables()[0].getIndex(0).getName());
+ assertEquals(index11.getType(), database.getSchema().getTables()[0].getIndex(0).getType());
+ assertEquals(index12.getName(), database.getSchema().getTables()[0].getIndex(1).getName());
+ assertEquals(index12.getType(), database.getSchema().getTables()[0].getIndex(1).getType());
+ assertEquals(index13.getName(), database.getSchema().getTables()[0].getIndex(2).getName());
+ assertEquals(index13.getType(), database.getSchema().getTables()[0].getIndex(2).getType());
+
+ assertEquals(table2.getName(), database.getSchema().getTables()[1].getName());
+ assertEquals(table2.getFieldCount(), database.getSchema().getTables()[1].getFieldCount());
+ assertEquals(field21.getName(), database.getSchema().getTables()[1].getField(0).getName());
+ assertEquals(field22.getName(), database.getSchema().getTables()[1].getField(1).getName());
+ assertEquals(field23.getName(), database.getSchema().getTables()[1].getField(2).getName());
+ assertEquals(table2.getIndexCount(), database.getSchema().getTables()[1].getIndexCount());
+ assertEquals(index21.getName(), database.getSchema().getTables()[1].getIndex(0).getName());
+ assertEquals(index21.getType(), database.getSchema().getTables()[1].getIndex(0).getType());
+ assertEquals(index22.getName(), database.getSchema().getTables()[1].getIndex(1).getName());
+ assertEquals(index22.getType(), database.getSchema().getTables()[1].getIndex(1).getType());
+ assertEquals(index23.getName(), database.getSchema().getTables()[1].getIndex(2).getName());
+ assertEquals(index23.getType(), database.getSchema().getTables()[1].getIndex(2).getType());
}
}
diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBUtil.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBUtil.java
index f55f1d7951..e23695ba82 100644
--- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBUtil.java
+++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBUtil.java
@@ -13,10 +13,11 @@ package org.eclipse.net4j.db;
import org.eclipse.net4j.db.ddl.IDBField;
import org.eclipse.net4j.db.ddl.IDBSchema;
import org.eclipse.net4j.db.ddl.IDBTable;
-import org.eclipse.net4j.internal.db.DBConnection;
-import org.eclipse.net4j.internal.db.DBInstance;
+import org.eclipse.net4j.internal.db.DBTransaction;
+import org.eclipse.net4j.internal.db.DBDatabase;
import org.eclipse.net4j.internal.db.DataSourceConnectionProvider;
import org.eclipse.net4j.internal.db.bundle.OM;
+import org.eclipse.net4j.spi.db.DBAdapter;
import org.eclipse.net4j.spi.db.DBSchema;
import org.eclipse.net4j.util.ReflectUtil;
import org.eclipse.net4j.util.io.ExtendedDataInput;
@@ -140,10 +141,10 @@ public final class DBUtil
/**
* @since 4.2
*/
- public static IDBInstance createInstance(IDBAdapter dbAdapter, IDBConnectionProvider dbConnectionProvider,
+ public static IDBDatabase createDatabase(IDBAdapter dbAdapter, IDBConnectionProvider dbConnectionProvider,
String schemaName)
{
- return new DBInstance(dbAdapter, dbConnectionProvider, schemaName);
+ return new DBDatabase((DBAdapter)dbAdapter, dbConnectionProvider, schemaName);
}
public static IDBSchema createSchema(String name)
@@ -206,9 +207,9 @@ public final class DBUtil
/**
* @since 4.2
*/
- public static Connection getSQLConnection(IDBConnection dbConnection)
+ public static Connection getSQLConnection(IDBTransaction dbConnection)
{
- return ((DBConnection)dbConnection).getSQLConnection();
+ return ((DBTransaction)dbConnection).getConnection();
}
public static Exception close(Connection connection)
diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IDBInstance.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IDBDatabase.java
index e9f47e2e4e..bf3f08e10f 100644
--- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IDBInstance.java
+++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IDBDatabase.java
@@ -19,21 +19,23 @@ import org.eclipse.net4j.util.container.IContainer;
* @noextend This interface is not intended to be extended by clients.
* @since 4.2
*/
-public interface IDBInstance extends IDBElement, IContainer<IDBConnection>
+public interface IDBDatabase extends IDBElement, IContainer<IDBTransaction>
{
public static final int DEFAULT_STATEMENT_CACHE_CAPACITY = 200;
- public IDBAdapter getDBAdapter();
+ public IDBAdapter getAdapter();
- public IDBSchema getDBSchema();
+ public IDBSchema getSchema();
- public IDBSchemaTransaction getDBSchemaTransaction();
+ public IDBConnectionProvider getConnectionProvider();
- public IDBConnectionProvider getDBConnectionProvider();
+ public IDBSchemaTransaction openSchemaTransaction();
- public IDBConnection openDBConnection();
+ public IDBSchemaTransaction getSchemaTransaction();
- public IDBConnection[] getDBConnections();
+ public IDBTransaction openTransaction();
+
+ public IDBTransaction[] getTransactions();
public int getStatementCacheCapacity();
diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IDBPreparedStatement.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IDBPreparedStatement.java
index 81e26db1a5..cb2c56d7d4 100644
--- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IDBPreparedStatement.java
+++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IDBPreparedStatement.java
@@ -22,7 +22,7 @@ import java.sql.SQLException;
*/
public interface IDBPreparedStatement extends IDBElement, Comparable<IDBPreparedStatement>, PreparedStatement
{
- public IDBConnection getDBConnection();
+ public IDBTransaction getTransaction();
public String getSQL();
diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IDBSchemaTransaction.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IDBSchemaTransaction.java
index 9245a08350..784112d005 100644
--- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IDBSchemaTransaction.java
+++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IDBSchemaTransaction.java
@@ -22,11 +22,13 @@ import org.eclipse.net4j.util.collection.Closeable;
*/
public interface IDBSchemaTransaction extends IDBElement, Closeable
{
- public IDBConnection getDBConnection();
+ public IDBDatabase getDatabase();
- public IDBSchema getDBSchema();
+ public IDBTransaction getTransaction();
- public IDBSchemaDelta getDBSchemaDelta();
+ public IDBSchema getSchema();
- public void commit();
+ public IDBSchemaDelta getSchemaDelta();
+
+ public IDBSchemaDelta commit();
}
diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IDBConnection.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IDBTransaction.java
index 6e720732eb..82494dcbea 100644
--- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IDBConnection.java
+++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/IDBTransaction.java
@@ -16,15 +16,15 @@ import org.eclipse.net4j.util.collection.Closeable;
/**
* @since 4.2
* @author Eike Stepper
- * @see DBUtil#getSQLConnection(IDBConnection)
+ * @see DBUtil#getSQLConnection(IDBTransaction)
* @noimplement This interface is not intended to be implemented by clients.
* @noextend This interface is not intended to be extended by clients.
*/
-public interface IDBConnection extends IDBElement, Closeable
+public interface IDBTransaction extends IDBElement, Closeable
{
- public IDBInstance getDBInstance();
+ public IDBDatabase getDatabase();
public IDBPreparedStatement getPreparedStatement(String sql, ReuseProbability reuseProbability);
- public IDBSchemaTransaction startSchemaTransaction();
+ public IDBSchemaTransaction openSchemaTransaction();
}
diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/ddl/IDBSchemaProvider.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/ddl/IDBSchemaProvider.java
deleted file mode 100644
index ec2e689680..0000000000
--- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/ddl/IDBSchemaProvider.java
+++ /dev/null
@@ -1,20 +0,0 @@
-/*
- * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Eike Stepper - initial API and implementation
- */
-package org.eclipse.net4j.db.ddl;
-
-/**
- * @author Eike Stepper
- * @since 4.2
- */
-public interface IDBSchemaProvider
-{
- public IDBSchema getDBSchema();
-}
diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBDatabase.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBDatabase.java
new file mode 100644
index 0000000000..4317203650
--- /dev/null
+++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBDatabase.java
@@ -0,0 +1,145 @@
+/*
+ * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Eike Stepper - initial API and implementation
+ */
+package org.eclipse.net4j.internal.db;
+
+import org.eclipse.net4j.db.DBUtil;
+import org.eclipse.net4j.db.DBUtil.RunnableWithConnection;
+import org.eclipse.net4j.db.IDBConnectionProvider;
+import org.eclipse.net4j.db.IDBDatabase;
+import org.eclipse.net4j.db.IDBTransaction;
+import org.eclipse.net4j.spi.db.DBAdapter;
+import org.eclipse.net4j.spi.db.DBSchema;
+import org.eclipse.net4j.util.container.IContainerDelta;
+import org.eclipse.net4j.util.container.SingleDeltaContainerEvent;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * @author Eike Stepper
+ */
+public final class DBDatabase extends DBElement implements IDBDatabase
+{
+ private final Set<DBTransaction> transactions = new HashSet<DBTransaction>();
+
+ private DBAdapter adapter;
+
+ private IDBConnectionProvider connectionProvider;
+
+ private DBSchema schema;
+
+ private DBSchemaTransaction schemaTransaction;
+
+ private int statementCacheCapacity = DEFAULT_STATEMENT_CACHE_CAPACITY;
+
+ public DBDatabase(DBAdapter adapter, IDBConnectionProvider dbConnectionProvider, final String schemaName)
+ {
+ this.adapter = adapter;
+ connectionProvider = dbConnectionProvider;
+
+ schema = DBUtil.execute(dbConnectionProvider, new RunnableWithConnection<DBSchema>()
+ {
+ public DBSchema run(Connection connection) throws SQLException
+ {
+ return (DBSchema)DBUtil.readSchema(schemaName, connection);
+ }
+ });
+
+ schema.lock();
+ }
+
+ public DBAdapter getAdapter()
+ {
+ return adapter;
+ }
+
+ public DBSchema getSchema()
+ {
+ return schema;
+ }
+
+ public DBSchemaTransaction openSchemaTransaction()
+ {
+ DBSchemaTransaction schemaTransaction = new DBSchemaTransaction(this);
+ this.schemaTransaction = schemaTransaction;
+ return schemaTransaction;
+ }
+
+ public void closeSchemaTransaction()
+ {
+ schemaTransaction = null;
+ }
+
+ public DBSchemaTransaction getSchemaTransaction()
+ {
+ return schemaTransaction;
+ }
+
+ public IDBConnectionProvider getConnectionProvider()
+ {
+ return connectionProvider;
+ }
+
+ public DBTransaction openTransaction()
+ {
+ DBTransaction transaction = new DBTransaction(this);
+ synchronized (transactions)
+ {
+ transactions.add(transaction);
+ }
+
+ fireEvent(new SingleDeltaContainerEvent<IDBTransaction>(this, transaction, IContainerDelta.Kind.ADDED));
+ return transaction;
+ }
+
+ public void closeTransaction(DBTransaction transaction)
+ {
+ synchronized (transactions)
+ {
+ transactions.remove(transaction);
+ }
+
+ fireEvent(new SingleDeltaContainerEvent<IDBTransaction>(this, transaction, IContainerDelta.Kind.REMOVED));
+ }
+
+ public DBTransaction[] getTransactions()
+ {
+ synchronized (transactions)
+ {
+ return transactions.toArray(new DBTransaction[transactions.size()]);
+ }
+ }
+
+ public boolean isEmpty()
+ {
+ synchronized (transactions)
+ {
+ return transactions.isEmpty();
+ }
+ }
+
+ public DBTransaction[] getElements()
+ {
+ return getTransactions();
+ }
+
+ public int getStatementCacheCapacity()
+ {
+ return statementCacheCapacity;
+ }
+
+ public void setStatementCacheCapacity(int statementCacheCapacity)
+ {
+ this.statementCacheCapacity = statementCacheCapacity;
+ }
+}
diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBInstance.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBInstance.java
deleted file mode 100644
index 3a1bd541db..0000000000
--- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBInstance.java
+++ /dev/null
@@ -1,140 +0,0 @@
-/*
- * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Eike Stepper - initial API and implementation
- */
-package org.eclipse.net4j.internal.db;
-
-import org.eclipse.net4j.db.DBUtil;
-import org.eclipse.net4j.db.DBUtil.RunnableWithConnection;
-import org.eclipse.net4j.db.IDBAdapter;
-import org.eclipse.net4j.db.IDBConnection;
-import org.eclipse.net4j.db.IDBConnectionProvider;
-import org.eclipse.net4j.db.IDBInstance;
-import org.eclipse.net4j.db.IDBSchemaTransaction;
-import org.eclipse.net4j.db.ddl.IDBSchema;
-import org.eclipse.net4j.spi.db.DBSchema;
-import org.eclipse.net4j.util.container.IContainerDelta;
-import org.eclipse.net4j.util.container.SingleDeltaContainerEvent;
-
-import java.sql.Connection;
-import java.sql.SQLException;
-import java.util.HashSet;
-import java.util.Set;
-
-/**
- * @author Eike Stepper
- */
-public final class DBInstance extends DBElement implements IDBInstance
-{
- private final Set<IDBConnection> dbConnections = new HashSet<IDBConnection>();
-
- private IDBAdapter dbAdapter;
-
- private IDBConnectionProvider dbConnectionProvider;
-
- private IDBSchema dbSchema;
-
- private DBSchemaTransaction dbSchemaTransaction;
-
- private int statementCacheCapacity = DEFAULT_STATEMENT_CACHE_CAPACITY;
-
- public DBInstance(IDBAdapter dbAdapter, IDBConnectionProvider dbConnectionProvider, final String schemaName)
- {
- this.dbAdapter = dbAdapter;
- this.dbConnectionProvider = dbConnectionProvider;
-
- dbSchema = DBUtil.execute(dbConnectionProvider, new RunnableWithConnection<IDBSchema>()
- {
- public IDBSchema run(Connection connection) throws SQLException
- {
- return DBUtil.readSchema(schemaName, connection);
- }
- });
-
- ((DBSchema)dbSchema).lock();
- }
-
- public IDBAdapter getDBAdapter()
- {
- return dbAdapter;
- }
-
- public IDBSchema getDBSchema()
- {
- return dbSchema;
- }
-
- public IDBSchemaTransaction getDBSchemaTransaction()
- {
- return dbSchemaTransaction;
- }
-
- public void setDBSchemaTransaction(DBSchemaTransaction dbSchemaTransaction)
- {
- this.dbSchemaTransaction = dbSchemaTransaction;
- }
-
- public IDBConnectionProvider getDBConnectionProvider()
- {
- return dbConnectionProvider;
- }
-
- public IDBConnection openDBConnection()
- {
- DBConnection dbConnection = new DBConnection(this);
- synchronized (dbConnections)
- {
- dbConnections.add(dbConnection);
- }
-
- fireEvent(new SingleDeltaContainerEvent<IDBConnection>(this, dbConnection, IContainerDelta.Kind.ADDED));
- return dbConnection;
- }
-
- public void closeDBConnection(DBConnection dbConnection)
- {
- synchronized (dbConnections)
- {
- dbConnections.remove(dbConnection);
- }
-
- fireEvent(new SingleDeltaContainerEvent<IDBConnection>(this, dbConnection, IContainerDelta.Kind.REMOVED));
- }
-
- public IDBConnection[] getDBConnections()
- {
- synchronized (dbConnections)
- {
- return dbConnections.toArray(new IDBConnection[dbConnections.size()]);
- }
- }
-
- public boolean isEmpty()
- {
- synchronized (dbConnections)
- {
- return dbConnections.isEmpty();
- }
- }
-
- public IDBConnection[] getElements()
- {
- return getDBConnections();
- }
-
- public int getStatementCacheCapacity()
- {
- return statementCacheCapacity;
- }
-
- public void setStatementCacheCapacity(int statementCacheCapacity)
- {
- this.statementCacheCapacity = statementCacheCapacity;
- }
-}
diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBPreparedStatement.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBPreparedStatement.java
index 3123256de3..6346e03c0b 100644
--- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBPreparedStatement.java
+++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBPreparedStatement.java
@@ -42,7 +42,7 @@ import java.util.Map;
*/
public final class DBPreparedStatement extends DBElement implements IDBPreparedStatement
{
- private final DBConnection dbConnection;
+ private final DBTransaction transaction;
private final String sql;
@@ -52,18 +52,18 @@ public final class DBPreparedStatement extends DBElement implements IDBPreparedS
private int touch;
- public DBPreparedStatement(DBConnection dbConnection, String sql, ReuseProbability reuseProbability,
+ public DBPreparedStatement(DBTransaction transaction, String sql, ReuseProbability reuseProbability,
PreparedStatement delegate)
{
- this.dbConnection = dbConnection;
+ this.transaction = transaction;
this.sql = sql;
this.reuseProbability = reuseProbability;
this.delegate = delegate;
}
- public DBConnection getDBConnection()
+ public DBTransaction getTransaction()
{
- return dbConnection;
+ return transaction;
}
public String getSQL()
@@ -105,7 +105,7 @@ public final class DBPreparedStatement extends DBElement implements IDBPreparedS
public void close() throws SQLException
{
- dbConnection.releasePreparedStatement(this);
+ transaction.releasePreparedStatement(this);
}
public IDBResultSet getGeneratedKeys() throws SQLException
diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBSchemaTransaction.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBSchemaTransaction.java
index 8ce4d64b11..b2a29f9ed9 100644
--- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBSchemaTransaction.java
+++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBSchemaTransaction.java
@@ -10,79 +10,112 @@
*/
package org.eclipse.net4j.internal.db;
-import org.eclipse.net4j.db.IDBAdapter;
-import org.eclipse.net4j.db.IDBConnection;
+import org.eclipse.net4j.db.DBException;
+import org.eclipse.net4j.db.DBUtil;
+import org.eclipse.net4j.db.DBUtil.RunnableWithConnection;
import org.eclipse.net4j.db.IDBSchemaTransaction;
-import org.eclipse.net4j.db.ddl.IDBSchema;
-import org.eclipse.net4j.db.ddl.delta.IDBSchemaDelta;
+import org.eclipse.net4j.internal.db.ddl.delta.DBSchemaDelta;
+import org.eclipse.net4j.spi.db.DBAdapter;
import org.eclipse.net4j.spi.db.DBSchema;
import java.sql.Connection;
+import java.sql.SQLException;
/**
* @author Eike Stepper
*/
-public final class DBSchemaTransaction extends DBElement implements IDBSchemaTransaction
+public final class DBSchemaTransaction extends DBElement implements IDBSchemaTransaction,
+ RunnableWithConnection<DBSchemaDelta>
{
- private DBConnection dbConnection;
+ private DBDatabase database;
- private IDBSchema dbSchema;
+ private DBTransaction transaction;
- public DBSchemaTransaction(DBConnection dbConnection)
+ private DBSchema oldSchema;
+
+ private DBSchema schema;
+
+ public DBSchemaTransaction(DBDatabase database)
+ {
+ this.database = database;
+
+ oldSchema = database.getSchema();
+ schema = new DBSchema(oldSchema);
+ }
+
+ public DBDatabase getDatabase()
{
- this.dbConnection = dbConnection;
+ return database;
+ }
- IDBSchema oldSchema = dbConnection.getDBInstance().getDBSchema();
- dbSchema = new DBSchema(oldSchema);
+ public DBTransaction getTransaction()
+ {
+ return transaction;
}
- public IDBConnection getDBConnection()
+ public void setTransaction(DBTransaction getTransaction)
{
- return dbConnection;
+ transaction = getTransaction;
}
- public IDBSchema getDBSchema()
+ public DBSchema getSchema()
{
- return dbSchema;
+ return schema;
}
- public IDBSchemaDelta getDBSchemaDelta()
+ public DBSchemaDelta getSchemaDelta()
{
- DBInstance dbInstance = dbConnection.getDBInstance();
- DBSchema oldSchema = (DBSchema)dbInstance.getDBSchema();
- return dbSchema.compare(oldSchema);
+ return (DBSchemaDelta)schema.compare(oldSchema);
}
- public void commit()
+ public DBSchemaDelta commit()
{
- DBInstance dbInstance = dbConnection.getDBInstance();
- DBSchema oldSchema = (DBSchema)dbInstance.getDBSchema();
- IDBSchemaDelta delta = dbSchema.compare(oldSchema);
+ if (transaction == null)
+ {
+ return DBUtil.execute(database.getConnectionProvider(), this);
+ }
- IDBAdapter dbAdapter = dbInstance.getDBAdapter();
- Connection connection = dbConnection.getSQLConnection();
+ try
+ {
+ Connection connection = transaction.getConnection();
+ return run(connection);
+ }
+ catch (SQLException ex)
+ {
+ throw new DBException(ex);
+ }
+ }
+
+ public DBSchemaDelta run(Connection connection) throws SQLException
+ {
+ DBSchemaDelta delta = getSchemaDelta();
try
{
oldSchema.unlock();
- dbAdapter.updateSchema(connection, oldSchema, delta);
+
+ DBAdapter adapter = database.getAdapter();
+ adapter.updateSchema(connection, oldSchema, delta);
}
finally
{
oldSchema.lock();
close();
}
+
+ return delta;
}
public void close()
{
- dbConnection.getDBInstance().setDBSchemaTransaction(null);
- dbConnection = null;
- dbSchema = null;
+ database.closeSchemaTransaction();
+ transaction = null;
+ oldSchema = null;
+ schema = null;
}
public boolean isClosed()
{
- return dbConnection == null;
+ return schema == null;
}
}
diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBConnection.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBTransaction.java
index 1e15d82c74..caa45f135b 100644
--- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBConnection.java
+++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/internal/db/DBTransaction.java
@@ -12,7 +12,7 @@ package org.eclipse.net4j.internal.db;
import org.eclipse.net4j.db.DBException;
import org.eclipse.net4j.db.DBUtil;
-import org.eclipse.net4j.db.IDBConnection;
+import org.eclipse.net4j.db.IDBTransaction;
import org.eclipse.net4j.db.IDBPreparedStatement;
import org.eclipse.net4j.db.IDBPreparedStatement.ReuseProbability;
import org.eclipse.net4j.db.IDBSchemaTransaction;
@@ -28,9 +28,9 @@ import java.util.TreeMap;
/**
* @author Eike Stepper
*/
-public final class DBConnection extends DBElement implements IDBConnection
+public final class DBTransaction extends DBElement implements IDBTransaction
{
- private final DBInstance dbInstance;
+ private final DBDatabase database;
private final NavigableMap<String, DBPreparedStatement> cache = new TreeMap<String, DBPreparedStatement>();
@@ -40,15 +40,15 @@ public final class DBConnection extends DBElement implements IDBConnection
private Connection connection;
- public DBConnection(DBInstance dbInstance)
+ public DBTransaction(DBDatabase database)
{
- this.dbInstance = dbInstance;
- connection = dbInstance.getDBConnectionProvider().getConnection();
+ this.database = database;
+ connection = database.getConnectionProvider().getConnection();
}
- public DBInstance getDBInstance()
+ public DBDatabase getDatabase()
{
- return dbInstance;
+ return database;
}
public void close()
@@ -56,7 +56,7 @@ public final class DBConnection extends DBElement implements IDBConnection
DBUtil.close(connection);
connection = null;
- dbInstance.closeDBConnection(this);
+ database.closeTransaction(this);
}
public boolean isClosed()
@@ -64,7 +64,7 @@ public final class DBConnection extends DBElement implements IDBConnection
return connection != null;
}
- public Connection getSQLConnection()
+ public Connection getConnection()
{
return connection;
}
@@ -106,17 +106,17 @@ public final class DBConnection extends DBElement implements IDBConnection
throw new IllegalStateException(sql + " already in cache"); //$NON-NLS-1$
}
- if (cache.size() > dbInstance.getStatementCacheCapacity())
+ if (cache.size() > database.getStatementCacheCapacity())
{
DBPreparedStatement old = cache.remove(cache.firstKey());
DBUtil.close(old.getDelegate());
}
}
- public IDBSchemaTransaction startSchemaTransaction()
+ public IDBSchemaTransaction openSchemaTransaction()
{
- DBSchemaTransaction dbSchemaTransaction = new DBSchemaTransaction(this);
- dbInstance.setDBSchemaTransaction(dbSchemaTransaction);
- return dbSchemaTransaction;
+ DBSchemaTransaction schemaTransaction = database.openSchemaTransaction();
+ schemaTransaction.setTransaction(this);
+ return schemaTransaction;
}
}

Back to the top