my dog learned polymorphism*
The moose likes JSF and the fly likes JSF: How to establish Database Connection for entire application? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "JSF: How to establish Database Connection for entire application?" Watch "JSF: How to establish Database Connection for entire application?" New topic
Author

JSF: How to establish Database Connection for entire application?

Ray Cheung
Greenhorn

Joined: Dec 15, 2008
Posts: 4
Hi all,

I'm using JBoss (Tomcat 5.5 inside), HSQLDB and JSF to build a web application. My question is how to establish a database connection to the HSQLDB inside JBoss shared throughout the entire life of my web application. My application is built using JSF. I tried making a ServletContextListener and put the connection object to the ServletContext, but then I can't get it back in the JSF page... I also tried to make a class with static object holding the connection object, but then my JBoss even can't start up at all after I deploy my application onto it.

Please advise for whatever possible solution. This problem stuck me long enough. Thanks so much.

ray.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15960
    
  19

Ah, grasshopper, you have much to learn...

First and foremost: A web application is a multi-user application. A Connection object can only be used by a single thread. So what you need is either synchronized acccess (which is VERY bad for performance for something like this) or a database connection pool so that you can keep several connections and pass them out as needed.

You can define a connection pool for JBoss and use JNDI to locate it for use. However if you're going to use an enterprise framework like JSF, you really probably ought to be using an enterprise ORM system such as Hibernate or EJB/JPA instead of doing low-level JDBC stuff.

The nice thing about JPA is that JBoss will actually inject the JPA entityManager directly into your code, so you no longer have to worry about the details. There are other benefits as well, such as maintainability and performance.


Customer surveys are for companies who didn't pay proper attention to begin with.
Ray Cheung
Greenhorn

Joined: Dec 15, 2008
Posts: 4
Thanks so much Tim Holloway. I made my app using JNDI to catch the DataSource now. I suspect if it is using connection pooling automatically, I have already configured maximum connection and so settings on JBoss.

For Hibernate, I read a little bit about it. I didn't consider using it because our app is built on another app which provides some WSDL API for us. We basically map our features onto that WSDL calls. For some very particular functions, we use our own database, which is very light-loading.

Thanks so much for your enlightenment.


Originally posted by Tim Holloway:
Ah, grasshopper, you have much to learn...

First and foremost: A web application is a multi-user application. A Connection object can only be used by a single thread. So what you need is either synchronized acccess (which is VERY bad for performance for something like this) or a database connection pool so that you can keep several connections and pass them out as needed.

You can define a connection pool for JBoss and use JNDI to locate it for use. However if you're going to use an enterprise framework like JSF, you really probably ought to be using an enterprise ORM system such as Hibernate or EJB/JPA instead of doing low-level JDBC stuff.

The nice thing about JPA is that JBoss will actually inject the JPA entityManager directly into your code, so you no longer have to worry about the details. There are other benefits as well, such as maintainability and performance.

[ December 15, 2008: Message edited by: Ray Cheung ]
 
jQuery in Action, 2nd edition
 
subject: JSF: How to establish Database Connection for entire application?
 
Similar Threads
getting errors while running duke's bank application
Create an instance of database in HSQLDB
how to create a database instance in HSQLDB
HSQLDB - Java Database Engine
Problems with running JSF in Eclipse 3.1