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 2004/01/04 02:59:34 UTC
cvs commit: db-ojb/src/java/org/apache/ojb/odmg/collections DListEntry_2.java DListImpl_2.java
arminw 2004/01/03 17:59:34
Modified: src/java/org/apache/ojb/odmg/collections DListEntry_2.java
DListImpl_2.java
Log:
fix bug in DListImpl_2 when materialize stored lists
Revision Changes Path
1.3 +78 -41 db-ojb/src/java/org/apache/ojb/odmg/collections/DListEntry_2.java
Index: DListEntry_2.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/odmg/collections/DListEntry_2.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- DListEntry_2.java 13 Dec 2003 14:34:23 -0000 1.2
+++ DListEntry_2.java 4 Jan 2004 01:59:34 -0000 1.3
@@ -59,14 +59,16 @@
import org.apache.ojb.broker.OJBRuntimeException;
import org.apache.ojb.broker.PBKey;
import org.apache.ojb.broker.PersistenceBroker;
+import org.apache.ojb.broker.util.logging.LoggerFactory;
+import org.apache.ojb.broker.util.logging.Logger;
import org.apache.ojb.odmg.TransactionExt;
import org.apache.ojb.odmg.TxManagerFactory;
+import org.apache.ojb.odmg.PBCapsule;
import java.io.Serializable;
/**
- * Insert the type's description here.
- * Creation date: (28.01.2001 21:23:26)
+ *
* @author Thomas Mahler
* @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
* @version $Id$
@@ -74,10 +76,15 @@
public class DListEntry_2 implements Serializable
{
/*
- declare transient or do we adopt persistent object is serialzable?
+ * declare transient, because ManageableCollection entries need to be {@link java.io.Serializable}.
+ */
+ private transient Logger log;
+ /*
+ TODO: declare transient or do we adopt persistent object is serialzable?
*/
protected Object realSubject;
protected DListImpl_2 dList;
+ protected PBKey pbKey;
protected Integer id;
protected Integer dlistId;
@@ -85,11 +92,28 @@
protected int position;
/**
- * Used to instantiate persistent DLists from DB
+ * Used to instantiate persistent DLists from DB by the kernel
+ * FOR INTERNAL USE ONLY
*/
public DListEntry_2()
{
super();
+ /*
+ arminw:
+ When PB kernel fill DList with DListEntry, the DListEntry needs to know the current
+ used PBKey, because we need to lookup the real objects when user iterates the list,
+ thus we need the associated PBKey to find right PB/DB connection.
+ TODO: Find a better solution
+ */
+ TransactionExt tx = TxManagerFactory.instance().getTransaction();
+ if(tx == null)
+ {
+ getLog().info("Can't find running transaction to lookup current associated PBKey");
+ }
+ else
+ {
+ this.pbKey = tx.getBroker().getPBKey();
+ }
}
/**
@@ -98,11 +122,21 @@
public DListEntry_2(DListImpl_2 dList, Object theObject)
{
this.dList = dList;
+ this.pbKey = dList != null ? dList.getPBKey() : null;
this.dlistId = dList.getId();
this.position = dList.size();
this.realSubject = theObject;
}
+ protected Logger getLog()
+ {
+ if (log == null)
+ {
+ log = LoggerFactory.getLogger(DListEntry_2.class);
+ }
+ return log;
+ }
+
protected void prepareForPersistency(PersistenceBroker broker)
{
if (oid == null)
@@ -120,34 +154,23 @@
public PBKey getPBKey()
{
- return dList != null ? dList.getPBKey() : null;
- }
-
- /**
- * Insert the method's description here.
- * Creation date: (06.02.2001 22:06:55)
- * @return int
- */
- public int getPosition()
- {
- return position;
+ if(pbKey == null)
+ {
+ if(dList != null)
+ {
+ pbKey = dList.getPBKey();
+ }
+ }
+ return pbKey;
}
- /**
- * Insert the method's description here.
- * Creation date: (30.01.2001 22:32:10)
- * @return java.lang.Object
- */
protected void prepareRealSubject(PersistenceBroker broker)
{
- if (realSubject == null)
+ if (oid == null)
{
- if (oid == null)
- {
- throw new OJBRuntimeException("can not return real object, real object and Identity is null");
- }
- realSubject = broker.getObjectByIdentity(oid);
+ throw new OJBRuntimeException("can not return real object, real object and Identity is null");
}
+ realSubject = broker.getObjectByIdentity(oid);
}
public Object getRealSubject()
@@ -163,29 +186,43 @@
{
prepareRealSubject(tx.getBroker());
}
+ else
+ {
+ PBKey pbKey = getPBKey();
+ if(pbKey != null)
+ {
+ PBCapsule capsule = new PBCapsule(pbKey, null);
+ try
+ {
+ prepareRealSubject(capsule.getBroker());
+ }
+ finally
+ {
+ capsule.destroy();
+ }
+ }
+ else
+ {
+ getLog().warn("No tx, no PBKey - can't materialise object with Identity " + getOid());
+ }
+ }
}
return realSubject;
}
+ public void setRealSubject(Object realSubject)
+ {
+ this.realSubject = realSubject;
+ }
- /**
- * Insert the method's description here.
- * Creation date: (06.02.2001 22:06:55)
- * @param newPosition int
- */
- public void setPosition(int newPosition)
+ public int getPosition()
{
- position = newPosition;
+ return position;
}
- /**
- * Insert the method's description here.
- * Creation date: (10.02.2001 17:58:45)
- * @param realSubject java.lang.Object
- */
- void setRealSubject(Object realSubject)
+ public void setPosition(int newPosition)
{
- this.realSubject = realSubject;
+ position = newPosition;
}
/**
1.3 +56 -40 db-ojb/src/java/org/apache/ojb/odmg/collections/DListImpl_2.java
Index: DListImpl_2.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/odmg/collections/DListImpl_2.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- DListImpl_2.java 28 Nov 2003 14:39:59 -0000 1.2
+++ DListImpl_2.java 4 Jan 2004 01:59:34 -0000 1.3
@@ -54,6 +54,12 @@
* <http://www.apache.org/>.
*/
+import java.util.AbstractList;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ListIterator;
+
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.ojb.broker.ManageableCollection;
import org.apache.ojb.broker.OJBRuntimeException;
@@ -72,6 +78,7 @@
import org.apache.ojb.odmg.PBCapsule;
import org.apache.ojb.odmg.TransactionImpl;
import org.apache.ojb.odmg.TxManagerFactory;
+import org.apache.ojb.odmg.TransactionExt;
import org.apache.ojb.odmg.oql.OQLQueryImpl;
import org.odmg.DArray;
import org.odmg.DCollection;
@@ -81,13 +88,6 @@
import org.odmg.QueryInvalidException;
import org.odmg.Transaction;
-import java.util.AbstractList;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-import java.util.ListIterator;
-import java.util.Vector;
-
/**
*
@@ -112,6 +112,7 @@
/**
* Used by PB-Kernel to instantiate ManageableCollections
+ * FOR INTERNAL USE ONLY
*/
public DListImpl_2()
{
@@ -143,43 +144,30 @@
return new DListEntry_2(this, obj);
}
- public PBKey getPBKey()
- {
- return pbKey;
- }
-
- public void setPBKey(PBKey pbKey)
+ private boolean checkForOpenTransaction(TransactionExt tx)
{
- this.pbKey = pbKey;
+ boolean result = false;
+ if(tx != null && tx.isOpen())
+ {
+ result = true;
+ if(pbKey == null) pbKey = tx.getBroker().getPBKey();
+ }
+ return result;
}
- public int hashCode()
+ public PBKey getPBKey()
{
- int hashCode = 1;
- Iterator it = elements.iterator();
- while (it.hasNext())
+ if(pbKey == null)
{
- Object obj = it.next();
- hashCode = 31 * hashCode + (obj == null ? 0 : obj.hashCode());
+ TransactionExt tx = TxManagerFactory.instance().getTransaction();
+ if(tx != null) checkForOpenTransaction(tx);
}
- return hashCode;
+ return pbKey;
}
- public String toString()
+ public void setPBKey(PBKey pbKey)
{
- ToStringBuilder buf = new ToStringBuilder(this);
- buf.append("id", id);
- buf.append("pbKey", pbKey);
- buf.append("size", size);
- buf.append("[containing elements: ");
- Iterator it = elements.iterator();
- while (it.hasNext())
- {
- Object obj = it.next();
- buf.append(obj != null ? obj.toString() : null);
- }
- buf.append("]");
- return buf.toString();
+ this.pbKey = pbKey;
}
/**
@@ -207,7 +195,7 @@
this.size++;
// if we are in a transaction: acquire locks !
TransactionImpl tx = TxManagerFactory.instance().getTransaction();
- if ((tx != null) && (tx.isOpen()))
+ if (checkForOpenTransaction(tx))
{
tx.lock(this, Transaction.WRITE);
tx.lock(entry, Transaction.WRITE);
@@ -253,7 +241,7 @@
DListEntry_2 entry = (DListEntry_2) elements.get(index);
// if we are in a transaction: acquire locks !
TransactionImpl tx = TxManagerFactory.instance().getTransaction();
- if ((tx != null) && (tx.isOpen()))
+ if (checkForOpenTransaction(tx))
{
tx.markDelete(entry);
}
@@ -309,7 +297,6 @@
return false;
else
return true;
-
}
/**
@@ -496,6 +483,35 @@
}
+ public int hashCode()
+ {
+ int hashCode = 1;
+ Iterator it = elements.iterator();
+ while (it.hasNext())
+ {
+ Object obj = it.next();
+ hashCode = 31 * hashCode + (obj == null ? 0 : obj.hashCode());
+ }
+ return hashCode;
+ }
+
+ public String toString()
+ {
+ ToStringBuilder buf = new ToStringBuilder(this);
+ buf.append("id", id);
+ buf.append("pbKey", pbKey);
+ buf.append("size", size);
+ buf.append("[containing elements: ");
+ Iterator it = elements.iterator();
+ while (it.hasNext())
+ {
+ Object obj = it.next();
+ buf.append(obj != null ? obj.toString() : null);
+ }
+ buf.append("]");
+ return buf.toString();
+ }
+
/**
* Access all of the elements of the collection that evaluate to true for the
* provided query predicate.
@@ -586,7 +602,7 @@
* Sets the elements.
* @param elements The elements to set
*/
- public void setElements(Vector elements)
+ public void setElements(List elements)
{
this.elements = elements;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org