RE: Newbie: Where to put instance for database access?
Should it be stored in the ServletContext for application scope? But how do I access this instance from my Actions? getServlet().getServletContext().getAttribute(xxx) Or would it be better to use data-sources? If I should use them, does anybody know a good tutorial on the internet? Perhaps hibernate would be a good choice too, but I think its a bit oversized for my application. You see - I am a total newbie concerning the use of databases with struts. Please help!!! Peter - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Newbie: Where to put instance for database access?
Yes. Check with servlet-APIdoc... Alexander -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Sent: Wednesday, July 06, 2005 11:10 AM To: user@struts.apache.org Subject: AW: Newbie: Where to put instance for database access? Should it be stored in the ServletContext for application scope? But how do I access this instance from my Actions? getServlet().getServletContext().getAttribute(xxx) is it the same as request.getSession().getServletContext().getAttribute(xxx) ? Peter - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Newbie: Where to put instance for database access?
Create a singleton class, that holds the instance of a connection in a ThreadLocal class in that singleton -- this way you don't have to keep stuff in the session... Regards, Alex. On 7/6/05, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Hi! I have maybe a simple question: My web-app has to get data out of a database. I have a class DatabaseHandler which holds a connection to the database and provides methods to query it. I would like to have only one instance of this class in my web-app. But I don't know where to put it. Should it be stored in the ServletContext for application scope? But how do I access this instance from my Actions? Or would it be better to use data-sources? If I should use them, does anybody know a good tutorial on the internet? Perhaps hibernate would be a good choice too, but I think its a bit oversized for my application. You see - I am a total newbie concerning the use of databases with struts. Please help!!! Peter - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Newbie: Where to put instance for database access?
You should be using some type of data source or preferrably a connection pool based on a data source. Most web servers include a connection pool implementation and all that's needed is to define the connection pool, define the data source, and then link the two. Your web app will then get a connection from the connection pool when needed, and release it when finished. There is not much overhead when using a connection pool as the connections are created once and then passed around to whomever needs them. You can also specify how many concurrent connections to the database the connection pool contains. Regardless of implementation, each request should be getting and releasing it's own connection, not sharing them between requests as your question seems be saying. If you are sharing a single connection, you will have multiple requests in your web application using the same connection object, which is not allowed and will cause you problems at some point. Since threads are non-deterministic, these errors will be mysterious, hard to duplicate, and very difficult to debug. See http://en.wikipedia.org/wiki/Race_condition#Computing -ed On 7/6/05, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Hi! I have maybe a simple question: My web-app has to get data out of a database. I have a class DatabaseHandler which holds a connection to the database and provides methods to query it. I would like to have only one instance of this class in my web-app. But I don't know where to put it. Should it be stored in the ServletContext for application scope? But how do I access this instance from my Actions? Or would it be better to use data-sources? If I should use them, does anybody know a good tutorial on the internet? Perhaps hibernate would be a good choice too, but I think its a bit oversized for my application. You see - I am a total newbie concerning the use of databases with struts. Please help!!! Peter - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Newbie: Where to put instance for database access?
Depending on the level of usage to the database, I would recommend implementing a connection-pool and requesting a connection each time you need to query the database. If you hold a single connection for your application, you will limit your scalability and hurt performance based on the user load. How you set up this pool is dependent on the database you are using and the resources you have available to that database. I would recommend looking at the Jakarta Commons-pool project. All of the major J2EE Appservers and most of the Java Servlet Container Servers support connection pooling. The only exception that I can think of off the top of my head is Sun iPlanet 6.0 or less. Tomcat will support connection pooling and has a decent how-to on their website. Aleksandar Matijaca [EMAIL PROTECTED] Aleksandar Matijaca [EMAIL PROTECTED] 07/06/2005 03:01 PM Please respond to Struts Users Mailing List user@struts.apache.org To Struts Users Mailing List user@struts.apache.org cc Subject Re: Newbie: Where to put instance for database access? Create a singleton class, that holds the instance of a connection in a ThreadLocal class in that singleton -- this way you don't have to keep stuff in the session... Regards, Alex. On 7/6/05, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Hi! I have maybe a simple question: My web-app has to get data out of a database. I have a class DatabaseHandler which holds a connection to the database and provides methods to query it. I would like to have only one instance of this class in my web-app. But I don't know where to put it. Should it be stored in the ServletContext for application scope? But how do I access this instance from my Actions? Or would it be better to use data-sources? If I should use them, does anybody know a good tutorial on the internet? Perhaps hibernate would be a good choice too, but I think its a bit oversized for my application. You see - I am a total newbie concerning the use of databases with struts. Please help!!! Peter - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]