This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSF: How to establish Database Connection for entire application?

 
Ray Cheung
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 17622
39
Android Eclipse IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ray Cheung
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic