如何在weblogic中实现单例

发布于 2024-10-07 07:44:10 字数 1267 浏览 0 评论 0原文

我尝试实现一个单例类方法,该方法由过滤器类调用。 过滤器类和其余类作为共享库添加到 WLS。

我有两个独立的 Web 应用程序正在运行 - 带过滤器的 servlet - 都在同一台托管服务器上。

所以一切都工作正常,除了单例被实例化两次。 请在代码片段下面找到。

public class Test
{
   private static Test ref ;

   private DataSource X;  
   static int Y;
   long Z ;   


   private Test ()
   {
      // Singleton 
   Z= 100 ;
   }

   public static synchronized Test getinstance()  throws NamingException, SQLException
   {
      if(ref == null)
      {         
         ref = new Test() ;         
         InitialContext ic = new InitialContext();

         ref.X = (DataSource)ic.lookup ("jdbc/Views");
      } 
      return ref ;    
   }

   public Object clone()throws CloneNotSupportedException
   {
       throw new CloneNotSupportedException(); 
   }

   public int sampleMethod (int X) throws SQLException
   {
   }

}

过滤方法:

public final class Filter implements Filter
{
 public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException
 {
  try
  {  
   Test ref = Test.getinstance();
   log.logNow(ref.toString());
   .......

  }
 }
}

在日志中我得到两个不同的引用 - 比如说

Test@f1a2e06 Test@f180f10

我在这里做错了什么吗?任何帮助都会很棒。

I tried implementing a singleton class method , which is invoked by a filter class.
The filter class and the rest of the classes , are added to WLS as a shared library.

And i have two separate web apps running - servlets with filter - both on the same managed server.

So everything is working fine except , the singleton is getting instantiated twice .
plz find below the code snippet.

public class Test
{
   private static Test ref ;

   private DataSource X;  
   static int Y;
   long Z ;   


   private Test ()
   {
      // Singleton 
   Z= 100 ;
   }

   public static synchronized Test getinstance()  throws NamingException, SQLException
   {
      if(ref == null)
      {         
         ref = new Test() ;         
         InitialContext ic = new InitialContext();

         ref.X = (DataSource)ic.lookup ("jdbc/Views");
      } 
      return ref ;    
   }

   public Object clone()throws CloneNotSupportedException
   {
       throw new CloneNotSupportedException(); 
   }

   public int sampleMethod (int X) throws SQLException
   {
   }

}

Filter method:

public final class Filter implements Filter
{
 public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException
 {
  try
  {  
   Test ref = Test.getinstance();
   log.logNow(ref.toString());
   .......

  }
 }
}

In the log am getting two different references - say

Test@f1a2e06 Test@f180f10

Am i doing something wrong here ? Any help would be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萧瑟寒风 2024-10-14 07:44:10

Servlet 容器针对不同的应用程序使用不同的类加载器。所以我不相信可以共享实例。您可以做的也许是通过 JNDI 注册实例。

A servlet container uses different classloaders for different application. So I don't believe it is possible to share an instance. What you can do is perhaps register the instance via JNDI.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文