You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by ar...@apache.org on 2007/10/29 12:38:41 UTC
svn commit: r589586 - in
/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg:
DatabaseImpl.java ImplementationExt.java ImplementationImpl.java
ImplementationJTAImpl.java TransactionImpl.java
Author: arminw
Date: Mon Oct 29 04:38:41 2007
New Revision: 589586
URL: http://svn.apache.org/viewvc?rev=589586&view=rev
Log:
add support to use the ODMG-api within the PB-api
Modified:
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/DatabaseImpl.java
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/ImplementationExt.java
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/ImplementationImpl.java
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/ImplementationJTAImpl.java
db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/TransactionImpl.java
Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/DatabaseImpl.java
URL: http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/DatabaseImpl.java?rev=589586&r1=589585&r2=589586&view=diff
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/DatabaseImpl.java (original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/DatabaseImpl.java Mon Oct 29 04:38:41 2007
@@ -86,6 +86,21 @@
return this.isOpen;
}
+ public synchronized void open(PBKey key) throws ODMGException
+ {
+ if(key != null)
+ {
+ pbKey = key;
+ isOpen = true;
+ //register opened database
+ odmg.registerOpenDatabase(this);
+ }
+ else
+ {
+ throw new DatabaseNotFoundException("OJB can't open database using 'NULL' as PBKey");
+ }
+ }
+
/**
* Open the named database with the specified access mode.
* Attempts to open a database when it has already been opened will result in
@@ -118,10 +133,7 @@
broker = PersistenceBrokerFactory.createPersistenceBroker(
BrokerHelper.extractAllTokens(name));
}
- pbKey = broker.getPBKey();
- isOpen = true;
- //register opened database
- odmg.registerOpenDatabase(this);
+ open(broker.getPBKey());
if (log.isDebugEnabled()) log.debug("Open database using PBKey " + pbKey);
}
catch (PBFactoryException ex)
Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/ImplementationExt.java
URL: http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/ImplementationExt.java?rev=589586&r1=589585&r2=589586&view=diff
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/ImplementationExt.java (original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/ImplementationExt.java Mon Oct 29 04:38:41 2007
@@ -21,6 +21,8 @@
import org.odmg.Implementation;
+import org.odmg.Transaction;
+import org.apache.ojb.broker.PersistenceBroker;
/**
* Offers useful none odmg-standard methods of the odmg {@link org.odmg.Implementation} interface.
@@ -171,35 +173,17 @@
*/
public void setOptimizedTransientObjectDetection(boolean optimizedTransientObjectDetection);
-// /**
-// * If set <em>true</em> the odmg implementation do it's best to find out the user intension, if set
-// * <em>false</em> OJB use an optimized mode and the user has to adhere strictly the odmg-api:
-// * <ul>
-// * <li>
-// * New objects can only be made persistent by using {@link org.odmg.Database#makePersistent(Object)}
-// * </li>
-// * <li>
-// * Only persistent objects can be locked with {@link org.odmg.Transaction#lock(Object, int)}.
-// * </li>
-// * <li>
-// * When deleting an object with {@link org.odmg.Database#deletePersistent(Object)} to reuse it
-// * within a transaction a call to {@link org.odmg.Database#makePersistent(Object)} is needed and
-// * field changes on objects marked as "deleted" are not allowed.
-// * </li>
-// * </ul>
-// * When running odmg in <em>safe-mode</em> these restrictions are "softened" and it's e.g. possible
-// * to persist new objects with {@link org.odmg.Transaction#lock(Object, int)}.
-// * <p/>
-// * The <em>optimized-mode</em> show a significant better performance, but needs strictness in using the API.
-// *
-// * @param safeMode Set <em>true</em> to enable the <em>safe-mode</em>, use <em>false</em> to enable
-// * the <em>optimized-mode</em>.
-// */
-// void setSafeMode(boolean safeMode);
-//
-// /**
-// * Returns <em>true</em> if this class use the safe-mode for
-// * user interaction, else the optimized-mode is used.
-// */
-// boolean isSafeMode();
+ /**
+ * Create new <code>Transaction</code> object and associate it with the current thread
+ * based on the specified broker instance.
+ *
+ * @param broker The current used broker instance.
+ * @param closeOnCommit If set <em>true</em> the injected broker instance will be closed on
+ * <code>tx.commit</code>.
+ * If set <em>false</em> the injected broker instance will be commited on tx.commit without
+ * a <code>PB.close()</code> call - the broker instance will be still useable.
+ * @return The newly created <code>Transaction</code> instance.
+ * @see org.odmg.Transaction
+ */
+ public Transaction newTransaction(PersistenceBroker broker, boolean closeOnCommit);
}
Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/ImplementationImpl.java
URL: http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/ImplementationImpl.java?rev=589586&r1=589585&r2=589586&view=diff
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/ImplementationImpl.java (original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/ImplementationImpl.java Mon Oct 29 04:38:41 2007
@@ -20,12 +20,15 @@
*/
+import org.apache.commons.lang.SystemUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.ojb.broker.Identity;
+import org.apache.ojb.broker.OJBRuntimeException;
import org.apache.ojb.broker.PBKey;
import org.apache.ojb.broker.PersistenceBroker;
import org.apache.ojb.broker.PersistenceBrokerFactory;
+import org.apache.ojb.broker.PersistenceBrokerInternal;
import org.apache.ojb.broker.util.collections.ManageableArrayList;
import org.apache.ojb.broker.util.configuration.Configuration;
import org.apache.ojb.broker.util.configuration.ConfigurationException;
@@ -52,10 +55,6 @@
/**
* Default implementation of the {@link Implementation} interface.
*
- * @author <a href="mailto:thma@apache.org">Thomas Mahler<a>
- * @author <a href="mailto:mattbaird@yahoo.com">Matthew Baird</a>
- * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
- *
* @version $Id$
*/
public class ImplementationImpl implements ImplementationExt
@@ -152,6 +151,11 @@
*/
public Transaction newTransaction()
{
+ return newInternTransaction();
+ }
+
+ TransactionImpl newInternTransaction()
+ {
if ((getCurrentDatabase() == null))
{
throw new DatabaseClosedException("Database is NULL, must have a DB in order to create a transaction");
@@ -164,6 +168,56 @@
catch (ConfigurationException e)
{
throw new ODMGRuntimeException("Error in configuration of TransactionImpl instance: " + e.getMessage());
+ }
+ return tx;
+ }
+
+ /**
+ * Create new <code>Transaction</code> object and associate it with the current thread.
+ *
+ * @param broker The current used broker instance.
+ * @param closeOnCommit If set <em>true</em> the injected broker instance will be closed on tx.commit.
+ * If set <em>false</em> the injected broker instance will be commited on tx.commit but is still open
+ * and prepared for use.
+ * @return The newly created <code>Transaction</code> instance.
+ * @see Transaction
+ */
+ public Transaction newTransaction(PersistenceBroker broker, boolean closeOnCommit)
+ {
+ TransactionImpl tx;
+ PBKey key = broker.getPBKey();
+ DatabaseImpl tmpDB;
+ try
+ {
+ if(currentDatabase == null)
+ {
+ tmpDB = new DatabaseImpl(this);
+ }
+ else
+ {
+ if(!currentDatabase.getPBKey().equals(key))
+ {
+ // database doesn't match
+ String msg = "Current active Database instance doesn't match the PBKey of" +
+ " the specified broker instance." + SystemUtils.LINE_SEPARATOR
+ + "Current PBKey: " + currentDatabase.getPBKey() + SystemUtils.LINE_SEPARATOR
+ + "Specified PBKey: " + key;
+ throw new OJBRuntimeException(msg);
+ }
+ tmpDB = currentDatabase;
+ }
+ if(!tmpDB.isOpen())
+ {
+ tmpDB.open(key);
+ }
+ tx = newInternTransaction();
+ tx.broker = (PersistenceBrokerInternal) broker;
+ tx.setCloseOnCommit(closeOnCommit);
+ }
+ catch (Exception e)
+ {
+ throw new OJBRuntimeException("Error while create intern Database and Transaction instance: "
+ + e.getMessage(), e);
}
return tx;
}
Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/ImplementationJTAImpl.java
URL: http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/ImplementationJTAImpl.java?rev=589586&r1=589585&r2=589586&view=diff
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/ImplementationJTAImpl.java (original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/ImplementationJTAImpl.java Mon Oct 29 04:38:41 2007
@@ -112,7 +112,7 @@
{
if (log.isDebugEnabled()) log.debug("beginInternTransaction was called");
J2EETransactionImpl tx = (J2EETransactionImpl) super.currentTransaction();
- if (tx == null) tx = newInternTransaction();
+ if (tx == null) tx = (J2EETransactionImpl) newInternTransaction();
if (!tx.isOpen())
{
// start the transaction
@@ -124,7 +124,7 @@
/**
* Returns a new intern odmg-transaction for the current database.
*/
- private J2EETransactionImpl newInternTransaction()
+ TransactionImpl newInternTransaction()
{
if (log.isDebugEnabled()) log.debug("obtain new intern odmg-transaction");
J2EETransactionImpl tx = new J2EETransactionImpl(this);
Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/TransactionImpl.java
URL: http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/TransactionImpl.java?rev=589586&r1=589585&r2=589586&view=diff
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/TransactionImpl.java (original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/odmg/TransactionImpl.java Mon Oct 29 04:38:41 2007
@@ -81,6 +81,7 @@
private final ImplementationImpl implementation;
private final NamedRootsMap namedRootsMap;
protected PersistenceBrokerInternal broker = null;
+ private boolean closeOnCommit;
private HashMap runtimeCascadeDeleteMap = new HashMap();
private HashMap runtimeDeleteOrphanMap = new HashMap();
@@ -146,6 +147,8 @@
this.implicitLocking = implementation.isImplicitLocking();
this.ordering = implementation.isOrdering();
this.optimizedTransientObjectDetection = implementation.isOptimizedTransientObjectDetection();
+ // by default we always close the used PB instance after commit.
+ this.closeOnCommit = true;
// assign a uniqe id to this tx
txGUID = guid.next();
curDB = implementation.getCurrentDatabase();
@@ -538,7 +541,7 @@
// clear the temporary used named roots map
// we should do that, because same tx instance
// could be used several times
- broker = null;
+ if(broker != null && broker.isClosed()) broker = null;
if(unmaterializedLocks.size() > 0) unmaterializedLocks.clear();
txStatus = Status.STATUS_NO_TRANSACTION;
}
@@ -753,7 +756,7 @@
if (log.isDebugEnabled()) log.debug("Commit transaction " + this);
}
// now do real commit on broker
- if(hasBroker() && getBroker().isInTransaction()) getBroker().commitTransaction();
+ if(hasBroker() && broker.isInTransaction()) broker.commitTransaction();
// Now, we notify everything the commit is done.
performTransactionAwareAfterCommit();
@@ -1266,12 +1269,25 @@
}
finally
{
- broker.close();
+ if(closeOnCommit)
+ {
+ broker.close();
+ }
broker = null;
}
}
}
+ boolean isCloseOnCommit()
+ {
+ return closeOnCommit;
+ }
+
+ void setCloseOnCommit(boolean closeOnCommit)
+ {
+ this.closeOnCommit = closeOnCommit;
+ }
+
/*
* @see Configurable#configure(Configuration)
*/
@@ -1525,14 +1541,14 @@
* Returns <em>true</em> if delete orphan is enabled for the specified
* single or collection descriptor.
*/
- protected boolean isDeleteOrphan(ObjectReferenceDescriptor ord)
+ protected boolean isDeleteOrphan(CollectionDescriptor cod)
{
boolean result;
- Boolean runtimeSetting = (Boolean) runtimeDeleteOrphanMap.get(ord);
+ Boolean runtimeSetting = (Boolean) runtimeDeleteOrphanMap.get(cod);
if(runtimeSetting == null)
{
- result = ord instanceof CollectionDescriptor && IRemovalAwareCollection.class.isAssignableFrom(
- getBrokerInternal().getCollectionTypes().getCollectionClass((CollectionDescriptor) ord));
+ result = IRemovalAwareCollection.class.isAssignableFrom(
+ getBrokerInternal().getCollectionTypes().getCollectionClass(cod));
}
else
{
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org