File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes where dose code of geting connection should put? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "where dose code of geting connection should put?" Watch "where dose code of geting connection should put?" New topic

where dose code of geting connection should put?

liang sun
Ranch Hand

Joined: May 12, 2001
Posts: 51
I have code that is related to connection pool.
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/myoracle");
conn = ds.getConnection();
Many of struts's action need call it.
I know 2 ways.
1) Codes is written in every action that use it.
2) encapsulating code into one class's static method.then every action class all call the class'static method
using 1), I fell CODE is redundancy!
using 2), when many user access application,if encapsulating class is single thread, performance will be bad.
or,if I have other choice?
[note]: Basically I Don't intend to use mutiple-tie structure,so I directly wirte sql into action class
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33107

You should try to avoid code redundancy as much as possible. A third alternative is to put the common code in an action superclass which your actions all extend. The new action would extend struts' action class.
A fourth alternative is to put all the data access code in a separate class. The action could instantiate this class and call it. There isn't a need to rely on a static method.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
I agree. Here's the link:
subject: where dose code of geting connection should put?
It's not a secret anymore!