[EJB 3.0] - Catching Throwable does not catch LazyInitializationExceptio
by flipmind
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
19Â years, 5Â months
[Persistence, JBoss/CMP, Hibernate, Database] - Create index for an @ManyToMany / @JoinTable relation
by literadix
Hi there,
I am trying to create a database index for a mapping table. My source looks like this:
| @ManyToMany(
| targetEntity=PortalUser.class,
| cascade = { CascadeType.PERSIST, CascadeType.MERGE },
| fetch = FetchType.LAZY
| )
| @JoinTable(
| name="friends",
| joinColumns={@JoinColumn(name="psid")},
| inverseJoinColumns={@JoinColumn(name="ptid")},
| uniqueConstraints={@UniqueConstraint(columnNames={"psid", "ptid"})}
| )
|
If I try to use the following code i get an error:
| @ManyToMany(
| targetEntity=PortalUser.class,
| cascade = { CascadeType.PERSIST, CascadeType.MERGE },
| fetch = FetchType.LAZY
| )
| @JoinTable(
| name="friends",
| joinColumns={@JoinColumn(name="psid")},
| inverseJoinColumns={@JoinColumn(name="ptid")},
| uniqueConstraints={@UniqueConstraint(columnNames={"psid", "ptid"})}
| )
| @Index(name = "friendindex", columnNames={"psid", "ptid"})
|
Does any one has a solution for this?
Thank you very much,
Maciej
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3988511#3988511
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3988511
19Â years, 5Â months
[EJB 3.0] - Re: Slow performance of queries and POJO mapping
by markvl2
Setting hibernate logging to WARN does help but there is still a signficant performance difference.
Here are the elapsed times with 20 consecutive selects each returning 5000 different rows (averaged over 3 runs excluding the 1st run). Hibernate logging set to WARN.
createQuery(hsql): 11088ms
createNativeQuery(sql): 5573ms
createNativeQuery(sql,class): 7417ms
JDBC: 5052ms
So createQuery is still more than twice as slow as direct JDBC, even with Hibernate logging disabled.
createNativeQuery(sql) is close to the JDBC time, but this excludes the time to generate the POJOs from the list of Objects returned.
Interestingly, createNativeQuery(sql,class) is considerable faster than createQuery even though the underlying query is essentially the same.
So perhaps the best approach is to go with createNativeQuery(sql,class) and then tune as required by rewriting as JDBC calls.
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3988510#3988510
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3988510
19Â years, 5Â months
[JBoss Eclipse IDE (users)] - EJB3 on Jboss 4.0.3 SP1+ Jboss IDE 1.5 - NameNotFoundExcepti
by sweetvenkat
Hi;
Can anybody help me with this? I am getting javax.naming.NameNotFoundException in ejb client program.
Here is my source code...
I am using...Jboss IDE for Eclipse 1.5
Jboss Application Server 4.0.3 sp1
HelloWorld.java
----------------------
package src;
import javax.ejb.Remote;
@Remote
public interface HelloWorld {
public void display();
}
HelloWorldBean.java
----------------------------
package src;
import javax.ejb.Stateless;
import src.HelloWorld;
public @Stateless class HelloWorldBean implements HelloWorld {
public void display() {
System.out.println("sairam");
}
}
HelloClient.java
---------------------
package src;
import javax.naming.*;
public class HelloClient {
public static void main(String[] args) throws Exception{
Context ct=new InitialContext();
HelloWorld hello=(HelloWorld)ct.lookup("HelloWorldBean/remote");
hello.display();
}
}
jndi.properties
---------------------
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099
I jar'ed the src/HelloWorld.class and src/HelloWorldBean and src/HelloClient and jndi.properties and deployed onto server.
When I tried to run the client program, i got the following exception...
Exception in thread "main" javax.naming.NameNotFoundException: HelloWorldBean not bound
at org.jnp.server.NamingServer.getBinding(NamingServer.java:514)
at org.jnp.server.NamingServer.getBinding(NamingServer.java:522)
at org.jnp.server.NamingServer.getObject(NamingServer.java:528)
at org.jnp.server.NamingServer.lookup(NamingServer.java:252)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:294)
at sun.rmi.transport.Transport$1.run(Transport.java:153)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:149)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:466)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:707)
at java.lang.Thread.run(Thread.java:595)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:247)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:223)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:126)
at org.jnp.server.NamingServer_Stub.lookup(Unknown Source)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:610)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:572)
at javax.naming.InitialContext.lookup(InitialContext.java:351)
at src.HelloClient.main(HelloClient.java:11)
Can you pls help me with the solution on how to bind to an EJB?
Thanks in inadvance.
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3988508#3988508
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3988508
19Â years, 5Â months
[JBoss Seam] - link action passes null instead of object
by modoc
I'm working off of some example code pasted in this forum for downloading pdfs, but I'm having issues. I have a datatable showing a list of attachments from an e-mail. I want the link to then download the file. Using the pdf download example in this forum. What is happening is the method on the seam component is getting called, but the attachment parameter is being passed in as null. No errors, it's just null.
Any ideas?
The download component code (POJO, not EJB):
| /**
| *
| */
| package com.digitalsanctuary.seam;
|
| import java.io.ByteArrayInputStream;
| import java.io.IOException;
|
| import javax.ejb.Remove;
| import javax.faces.context.ExternalContext;
| import javax.faces.context.FacesContext;
| import javax.servlet.ServletOutputStream;
| import javax.servlet.http.HttpServletResponse;
|
| import org.jboss.seam.ScopeType;
| import org.jboss.seam.annotations.Destroy;
| import org.jboss.seam.annotations.In;
| import org.jboss.seam.annotations.Logger;
| import org.jboss.seam.annotations.Name;
| import org.jboss.seam.annotations.Scope;
| import org.jboss.seam.log.Log;
|
| import com.digitalsanctuary.mail.message.Attachment;
|
| /**
| * @author modoc
| *
| */
| @Name("downloadAttachment")
| @Scope(ScopeType.SESSION)
| public class DownloadAttachment {
| @In(value = "#{facesContext.externalContext}")
| private ExternalContext mExternalContext;
|
| @In(value = "#{facesContext}")
| private FacesContext mFacesContext;
|
| @Logger
| private Log mLog;
|
| public String download(Attachment pAttachment) {
| if (pAttachment == null) {
| return null;
| }
| int read = 0;
| byte[] bytes = new byte[1000];
|
| HttpServletResponse response = (HttpServletResponse) mExternalContext.getResponse();
| response.setContentType("application/octet");
| response.addHeader("Content-disposition", "attachment; filename=\"" + pAttachment.getFileName() + "\"");
|
| try {
| ServletOutputStream responseOutputStream = response.getOutputStream();
| ByteArrayInputStream attachmentInputStream = new ByteArrayInputStream(pAttachment.getContent());
| while ((read = attachmentInputStream.read(bytes)) != -1) {
| responseOutputStream.write(bytes, 0, read);
| }
| responseOutputStream.flush();
| responseOutputStream.close();
| this.mFacesContext.responseComplete();
| } catch (IOException ioe) {
| mLog.error("IOException trying to send out attachment file", ioe);
| }
| return null;
| }
|
| @Destroy
| @Remove
| public void destroy() {
| }
|
| }
|
The relevant section of the xhtml:
| <h:outputText rendered="#{mailQueue.currentMessage.attachmentCount > 0}">
| <div id="toplinedblock">
| <strong>Attachments:</strong><br />
| <h:dataTable value="#{mailQueue.currentMessage.attachments}" var="attachment"
| rendered="#{mailQueue.currentMessage.attachmentCount>0}">
| <h:column>
| <f:facet name="header">Name</f:facet>
| <s:link value="#{attachment.fileName}" action="#{downloadAttachment.download(attachment)}" />
| </h:column>
| <h:column>
| <f:facet name="header">Size</f:facet>
| #{attachment.contentSizeInK} KB
| </h:column>
| </h:dataTable>
| </div>
| </h:outputText>
|
The attachment is definitely working correctly in the datatable, as all it's values appear correctly. When the link is clicked, the method gets called, but the pAttachment comes in as null into the java.
Thoughts?
Thanks!
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3988507#3988507
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3988507
19Â years, 5Â months