• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

closing connection, why must be done?

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much Damanjit, yours explanation was very clear
[ January 15, 2005: Message edited by: Marco Vanoli ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic