java - Approach to JDBC connections in a Servlet/JSP application -
I have some existing Java code like many variations of DAO classes, for example
Class Employee Dao {Public Employee Dow (connection connection)} // Prepare statements used for the methods given below. Public employee will get employee employment (long ID) {} public collection & lt; Employee & gt; GetEmployeesByDepartment (long department ID) {} ...} All DAO classes have been created with a JDBC connection. Prepare any statement needed by the organizer methods. It works fine in a threaded environment (eg batch processing) where the collar can make a connection and instantaneous doo objects with that connection can instantiate.
I want to be able to use it in Java web application, but I'm not sure how to manage JDBC connections. Here are some suggestions:
-
Each request creates a new JDBC connection and instantiates the necessary DAO objects. Both creation and Dao are clearly expensive from urgency
-
Each request receives a connection from a JNDI datasource and instantiates the necessary DAO objects. This connection terminates the upper part of the connection but retains the upper side of the preparation of all the statements.
-
Each request brings connections to a JDNI datasource and the necessary DAO objects But the statements have not been prepared by the producers, they have been made essentially necessary.
-
An HttpSessionListener accelerates the claims items and sets them in session (using setAttribute) when the session ends, the connection stops.
-
Create a Stateless Session Bean Bean will institute the necessary Dao objects.
Option 1 is not a real contender, it's just to show my thinking process.
Option 2 and 3 will work but look sub-optimal, preparing a statement on every request looks like an overhead which should be easy to avoid, but I think I remember That option
option 4 avoids being overwritten on every call, but on the cost of maintaining manually state. I do not think this is the desired usage pattern in servlet / JSP applications (keep connections for an extended period). Option 5 should work, but looks like a lot overhead and it means that I have EJB container instead of the full simple servlet engine
- Avoid preparing statements on every request if possible.
- Preferably use a simple service engine:
PS: I know that hibernation Being in and some can remove it from the structure like unit beans but I guess the "no frame" base case E do not want to.
I can find you in every major application server (or any decent implementation, such as you have a database connection DBCP, C3P, etc., recommend using the pool.)
In the context of your datasheets (more or less) the cash can do a good job in injection in the dow instance, so that You can start a bit more performance (over time as well as memory As Peter).
However, I say that you make your statements all the time and do not try to cash them, because they are still connected to the connection, and those connections are managed by the data source .
And in the end, about your concern at preparing for the statements: If you carefully write your questions, it may be downright not time is used in preparing the statement ; What is really heavy is the query scheme, which is done in the database, behind the scenes. This is one of the reasons that your 'first' execution of a query takes longer. However, if you write a parametric query in your statement, then the database will cache your query execution plan and after that every query will reuse it in execution. For example:
generated statement stmt = conn.prepareStatement ("Choose name from employees where number =?"); is cacheable, and every time you prepare this statement, the query plan will be reused (the database 'remember' which it is ready for another statement
However, it:
created location stmt = conn.prepareStatement ("Select the name from the staff where the number =" + employee number); This means that whenever you prepare a statement (this 'do not miss' query), you have to prepare a separate execution plan - the way the display problem I have created a query instead of the Java statement object. Not to mention that this way is unsafe and prone to SQL injection as well.
I hope you find this useful
Comments
Post a Comment