Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

where dose code of geting connection should put?

 
liang sun
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Sheriff
Posts: 35975
422
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!