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 Ken Hu <ke...@mmti.com.tw> on 2005/12/09 06:55:51 UTC

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

>  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 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
>
>