aspose file tools*
The moose likes JDBC 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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "where dose code of geting connection should put?" Watch "where dose code of geting connection should put?" New topic
Author

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?
Thanks
[note]: Basically I Don't intend to use mutiple-tie structure,so I directly wirte sql into action class
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29287
    
140

Liang,
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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: where dose code of geting connection should put?
 
Similar Threads
Oracle Data Source
Hi i am trying to connect to postgresql database using struts 1.2..i have used datasource
where dose code of geting connection should put
Difference between DataSource and ConnectionPoolDataSource
Standard way of connecting database in struts...