[jboss-user] [EJB 3.0] - Catching Throwable does not catch LazyInitializationExceptio

flipmind do-not-reply at jboss.com
Sat Nov 25 01:42:09 EST 2006


I have an entity class with a toString() method but it is failing with a LazyInitializationException.

How can isDAOAccessable() thrown an exception if it is catching Throwable?  I am using jboss-4.0.5.GA


@Entity
@Table(name = "STOCKITEM")
@IdClass(ProductKey.class)
public class Product implements Serializable {
	@Override
	public String toString()
	{
		String desc = "PRODUCTCODE:=" + this.getProductCode() + ", TITLE:=" + title +
		", RICHDESCRIPTION:=" + richDescription + ", ICONSSTR:=" + iconsstr + ", ORDER_LIMIT_QTY:=" +
		maximumItemPerOrder + ", PRODUCTVARIANTS:=[\n";
		if ( CommonUtils.isDAOAccessable(productVariants))
			for(ProductVariant pVar : productVariants)
				desc += pVar.toString() + "\n";
		else
			desc += "null";

	}
}


	/**
	 * Checks if a Collection returned using Hibernate has been loaded or 
	 * if it will cause a lazy loaded exception
	 * @param c
	 * @return
	 */
	public static boolean isDAOAccessable(Collection c) {
		try {
			System.out.println("WORD UP");
			if (c==null) {
				return false;
			}
			else {
				c.iterator().hasNext();
				return true; // It is ok
			}
		}
		catch (Throwable ex){
			return false;
		}
	}
	

This is the result

19:26:23,898 INFO  [STDOUT] WORD UP
19:26:23,898 ERROR [LazyInitializationException] failed to lazily initialize a c
ollection of role: estore.common.Product.productVariants, no s
ession or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collect
ion of role: estore.common.Product.productVariants, no session
 or session was closed
        at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitia
lizationException(AbstractPersistentCollection.java:358)
        at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitia
lizationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
        at org.hibernate.collection.AbstractPersistentCollection.initialize(Abst
ractPersistentCollection.java:343)
        at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPe
rsistentCollection.java:86)
        at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:16
3)
        at estore.common.util.CommonUtils.isDAOAccessable(Ljav
a.util.Collection;)Z(Unknown Source)
        at estore.common.Product.toString()Ljava.lang.String;(
Unknown Source)
        at org.apache.log4j.or.DefaultRenderer.doRender(DefaultRenderer.java:26)

        at org.apache.log4j.or.RendererMap.findAndRender(RendererMap.java:70)
        at org.apache.log4j.spi.LoggingEvent.getRenderedMessage(LoggingEvent.jav
a:288)
        at org.apache.log4j.helpers.PatternParser$BasicPatternConverter.convert(
PatternParser.java:395)
        at org.apache.log4j.helpers.PatternConverter.format(PatternConverter.jav
a:56)
        at org.apache.log4j.PatternLayout.format(PatternLayout.java:495)
        at org.apache.log4j.WriterAppender.subAppend(WriterAppender.java:292)
        at org.apache.log4j.WriterAppender.append(WriterAppender.java:150)

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3988512#3988512

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3988512



More information about the jboss-user mailing list