You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@axis.apache.org by Ron Reynolds <Ro...@RonReynolds.com> on 2005/12/08 21:04:39 UTC

Re: How to get database connection in my service via connection pool

you mean how do you pass it forward from your servlet to your service handler?
 i can see three ways - use a static ThreadLocal (singleton) to hold the
connection and thus associate a Connection with the thread - the same thread
will be used to call the service handler at which point it can call
MyThreadSpecificConnectionHolder.get() to get the connection assigned to the
thread back out.  OR you can put the connection into the MessageContext (which
itself is stored in a ThreadLocal) using its setProperty()/getProperty()
methods to pass it forward.

tho in most cases its the service-handler's code that calls
ConnectionPool.getConnection() when the connection is needed to avoid the
overhead and additional dependency created using the above 2 methods (the
first requires that you know that your thread's specific connection is stored
in a singleton and the second requires that some string/key be used by both
your servlet and your service handler).  if your service-handler doesn't need
a connection to process the request you've created added work for yourself
that doesn't provide any return on investment.

just my $0.02 worth...
.................ron.
> Dear All:
>
> I've wrote a small servlet that extends AxisServlet.
> This home-made servlet can get database connection from our connection
> pooling mechanism.
>
> The question is : When I write the java code of my service , how can I
> get a database connection ? I find no way to send a database connection
> to my service from my own AxisServlet.
>
> Any idea ?
>
> Ken
> --
> ¬ãµo³¡             ­J­««Â             Ken Hu          ken@mmti.com.tw
>
> ©sµØ¬ì§ÞªÑ¥÷¦³­­¤½¥q
> http://www.mmti.com.tw
>
> °ª¶¯É]804¹ª¤s°Ï½¬®ü¸ô70¸¹
> °ê¥ß¤¤¤s¤j¾Ç³Ð·s¨|¦¨¤¤¤ß511«Ç
>
> ¹q¸Ü      07-5253020
> ¶Ç¯u      07-5252165
> ¦æ°Ê      0937083880
>
>



Re: How to get database connection in my service via connection pool

Posted by Ron Reynolds <Ro...@RonReynolds.com>.
this code won't work:

> 	private static ThreadLocal tm_app = new ThreadLocal() {
>         protected synchronized Object getTMApp() {
>             return _app ;
>         }
>     };

because ThreadLocal doesn't define a getTMApp() method (the only way you could
invoke this method through the tm_app ref is through reflection).  my guess is
that you meant for this to be initialValue() (which is defined in
ThreadLocal), tho i think that might not be a good idea either...

if you're storing the app in a static within your CtuAxisServlet (i.e., if all
threads are using the same instance of Application) then why have a
ThreadLocal at all?  why not just have

public class CtuAxisServlet ... {
  ...
  public static Application getApplication() { return _app; }
  ...
}
?

and this raises the question - will there really be more than 1 Application
instance at a time (within the same JVM)?  if not it makes sense that it would
be a singelton which greatly simplifies this problem.



>>  use a static ThreadLocal (singleton) to hold the
>> connection and thus associate a Connection with the thread - the same thread
>> will be used to call the service handler at which point it can call
>> MyThreadSpecificConnectionHolder.get() to get the connection assigned to the
>> thread back out
>>
>
> Thanks for your suggestion.
> My servlet is like the following:
> -----------------------------------------------------------------------------------------------------------
> public class CtuAxisServlet extends AxisServlet {
> 	private static Application _app = null;
>
> 	public CtuAxisServlet() {
> 		super();
>     }
>
> 	public void init() throws javax.servlet.ServletException {
> 		super.init();
> 		ServletContext sContext = this.getServletContext();
>         	_app = (Application) sContext.getAttribute("Application");
> 	}
>
>
> 	private static ThreadLocal tm_app = new ThreadLocal() {
>         protected synchronized Object getTMApp() {
>             return _app ;
>         }
>     };
> }
> -----------------------------------------------------------------------------------------------------------
> A method name getConnection is encapsulated in _app object which is used
> to get a data connection from my connection pool.
>
> Right now , I still don't know how to get this _app object in my service
> class(A java class which is deployed onto axis as my web service).
> Could you please explain more to me ? Thanks !
>
> Ken
> --
> ¬ãµo³¡             ­J­««Â             Ken Hu          ken@mmti.com.tw
>
> ©sµØ¬ì§ÞªÑ¥÷¦³­­¤½¥q
> http://www.mmti.com.tw
>
> °ª¶¯É]804¹ª¤s°Ï½¬®ü¸ô70¸¹
> °ê¥ß¤¤¤s¤j¾Ç³Ð·s¨|¦¨¤¤¤ß511«Ç
>
> ¹q¸Ü      07-5253020
> ¶Ç¯u      07-5252165
> ¦æ°Ê      0937083880
>
>



Re: How to get database connection in my service via connection pool

Posted by Ken Hu <ke...@mmti.com.tw>.
>  use a static ThreadLocal (singleton) to hold the
> connection and thus associate a Connection with the thread - the same thread
> will be used to call the service handler at which point it can call
> MyThreadSpecificConnectionHolder.get() to get the connection assigned to the
> thread back out
> 

Thanks for your suggestion.
My servlet is like the following:
-----------------------------------------------------------------------------------------------------------
public class CtuAxisServlet extends AxisServlet {
	private static Application _app = null;
	
	public CtuAxisServlet() {
		super();
    }
	
	public void init() throws javax.servlet.ServletException {
		super.init();
		ServletContext sContext = this.getServletContext();
        	_app = (Application) sContext.getAttribute("Application");
	}
	
	
	private static ThreadLocal tm_app = new ThreadLocal() {
        protected synchronized Object getTMApp() {
            return _app ;
        }
    };
}
-----------------------------------------------------------------------------------------------------------
A method name getConnection is encapsulated in _app object which is used
to get a data connection from my connection pool.

Right now , I still don't know how to get this _app object in my service
class(A java class which is deployed onto axis as my web service).
Could you please explain more to me ? Thanks !

Ken
-- 
研發部             胡重威             Ken Hu          ken@mmti.com.tw

孟華科技股份有限公司
http://www.mmti.com.tw

高雄巿804鼓山區蓮海路70號
國立中山大學創新育成中心511室

電話      07-5253020
傳真      07-5252165
行動      0937083880


Re: How to get database connection in my service via connection pool

Posted by Guy Rixon <gt...@ast.cam.ac.uk>.
Perhaps the idea here is to hide the JNDI look-up of the connection factory in
the servlet, thereby making the service code cleaner? In that case, your ideas
would still apply, but it's the ConnectionPool itself that needs to be passed
to the service, rather than the a specific connection. If the OP has a service
handler that knows about the special connection pool but not about the servlet
environment, then this would work. It might might make the service handler
testable away from the J2EE environment, which would be good.

On Thu, 8 Dec 2005, Ron Reynolds wrote:

> you mean how do you pass it forward from your servlet to your service handler?
>  i can see three ways - use a static ThreadLocal (singleton) to hold the
> connection and thus associate a Connection with the thread - the same thread
> will be used to call the service handler at which point it can call
> MyThreadSpecificConnectionHolder.get() to get the connection assigned to the
> thread back out.  OR you can put the connection into the MessageContext (which
> itself is stored in a ThreadLocal) using its setProperty()/getProperty()
> methods to pass it forward.
>
> tho in most cases its the service-handler's code that calls
> ConnectionPool.getConnection() when the connection is needed to avoid the
> overhead and additional dependency created using the above 2 methods (the
> first requires that you know that your thread's specific connection is stored
> in a singleton and the second requires that some string/key be used by both
> your servlet and your service handler).  if your service-handler doesn't need
> a connection to process the request you've created added work for yourself
> that doesn't provide any return on investment.
>
> just my $0.02 worth...
> .................ron.
> > Dear All:
> >
> > I've wrote a small servlet that extends AxisServlet.
> > This home-made servlet can get database connection from our connection
> > pooling mechanism.
> >
> > The question is : When I write the java code of my service , how can I
> > get a database connection ? I find no way to send a database connection
> > to my service from my own AxisServlet.
> >
> > Any idea ?
> >
> > Ken
> > --
> > ¬ãµo³¡             ­J­««Â             Ken Hu          ken@mmti.com.tw
> >
> > ©sµØ¬ì§ÞªÑ¥÷¦³­­¤½¥q
> > http://www.mmti.com.tw
> >
> > °ª¶¯É]804¹ª¤s°Ï½¬®ü¸ô70¸¹
> > °ê¥ß¤¤¤s¤j¾Ç³Ð·s¨|¦¨¤¤¤ß511«Ç
> >
> > ¹q¸Ü      07-5253020
> > ¶Ç¯u      07-5252165
> > ¦æ°Ê      0937083880
> >
> >
>
>

Guy Rixon 				        gtr@ast.cam.ac.uk
Institute of Astronomy   	                Tel: +44-1223-337542
Madingley Road, Cambridge, UK, CB3 0HA		Fax: +44-1223-337523