aspose file tools*
The moose likes JBoss/WildFly and the fly likes closing connection, why must be done? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » JBoss/WildFly
Bookmark "closing connection, why must be done?" Watch "closing connection, why must be done?" New topic
Author

closing connection, why must be done?

Marco Vanoli
Ranch Hand

Joined: Jan 12, 2005
Posts: 99
On a book i read the following example (at the bottom). I imported it in a jar than run with jboss.

The error on interroga(String str) method is:

ON CLIENT
java.sql.SQLException: Connection handle has been closed and is unusable
ON SERVER
[CachedConnectionManager] Closing a connection for you. Please close them yourself: org.jboss.resource.adapter.jdbc.WrappedConnection@594680

The problem is the that is in the apriConnessione() i got a call for make a db connection. This one is activated fist from ejbActivate(); then jboss close automatically connection without a ejbPassivate(). Then i enter in Interroga() method and the connection is no more avaible giving me exception.

I read the FAQ , expecially http://www.jboss.org/wiki/Wiki.jsp?page=WhatDoesTheMessageDoYourOwnHousekeepingMean
and so i correct the exercise putting alla connection statemente in the method interroga():


In this case the program has no error.
BUT
I do not understand the beaviour of jboss because i studied that in ejbPassivate / ejbActivate i must close connections; not in every method!
Reading the FAQ i understand that i must open & close connection in every method.
Can you explain me why? .. or what i misunderstand
thankyou, Bye



bye, <br />Marco
Damanjit Kaur
Ranch Hand

Joined: Oct 18, 2004
Posts: 346
Hi,

ejbActivate() and ejbPassivate() are called during instance pooling. ejbPassivate() invoked when instance is sent back to pool and ejbActivate when instance taken from pool. Its recommended to release resources being held by bean before its passivated and hence closing of database connection and while activating the bean instance from pool the database resource connection can be opened.

If the database connection is opened only in ejbActivate() method which is not neccessary invoked before some other method on bean instance is called ( for a simple reason that bean instance hasn't been passivated as yet)then in that case this other method will not have connection opened.
Marco Vanoli
Ranch Hand

Joined: Jan 12, 2005
Posts: 99
Yes i understand what you say but if a put a connection to database in the client as follow is not right as putting a connection inside a method in the bean?

Damanjit Kaur
Ranch Hand

Joined: Oct 18, 2004
Posts: 346
Yes i understand what you say but if a put a connection to database in the client as follow is not right as putting a connection inside a method in the bean?

code:

class Client
...


sfjdbc.apriConnessione(); //method that in bean connect to Db

sfjdbc.interroga("select * from Cliente where COD_FISCALE = 'MRNFRZ68M29E463B'"); /*method that use the database, that give me error if do not put insit it the connections to db routines*/

sfjdbc.chiudiConnessione();//method that in bean disnect to Db



sfjdbc.remove();
...
}


In above code when you put connection inside client code, where are you storing the connection object, its still being stored in Session Bean and there you have declared this Connection object as transient type, so when the bean instance is passivated the Connection object value is not preserved. Also its recommended to release such resources when the bean instance is passivated and hence in your earlier code, the code to open n close Database connection in ejbActivate() and ejbPassivate() was justified.

The point is that you don't know when ejbActivate() and ejbPassivate() are called so its better if you open the database connection in interroga() and close it before coming out of this method. Closing a database connection only release the connection back to connection pooling and hence not an overhead if in each business methods its opened n closed again.
Marco Vanoli
Ranch Hand

Joined: Jan 12, 2005
Posts: 99
Thank you very much Damanjit, yours explanation was very clear
[ January 15, 2005: Message edited by: Marco Vanoli ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: closing connection, why must be done?