title: Can I have a singleton lifecycle webservice on glassfish V3?
body: <p>I have a simple webservice created with Netbeans 6.5 and deployed into 2 glassfish servers <strong>V2.1 and V3</strong>. The ws has a basic method GetInstanceID that I call 3 times from a client. </p>
  
  <pre><code>@WebService()
  public class FirstWS {
  
  private long m_instanceID = 0;   //instance id
  
  //Log
  private void WriteLog(String cadena){
      String msg = "";
      DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss:SSS");
      Date fecha = new Date();
      msg = dateFormat.format(fecha) + " ***** " + this.getClass().getName() + " (m_instanceID=" + m_instanceID + "): " + cadena +
            "           " + "ThreadID=" + Thread.currentThread().getId() + " ThreadName=" + Thread.currentThread().getName();
      Logger.getLogger(this.getClass().getName()).log(Level.INFO,msg);
  }
  
  //Constructor
  public FirstWS() {
      Random rnd = new Random();
      m_instanceID = rnd.nextLong();
      WriteLog("Executing Constructor");
  }
  
  //PostConstruct
  @PostConstruct
  public void ThisIsThePostConstruct() {
      WriteLog("Executing PostConstruct");
  }
  
  //PreDestroy
  @PreDestroy
  public void ThisIsThePreDestroy() {
      WriteLog("Executing PreDestroy");
  }
  
  //Method
  @WebMethod(operationName = "GetInstanceID")
  @WebResult(name="InstanceID")
  public long GetInstanceID() {
      //TODO write your implementation code here:
      WriteLog("Executing GetInstanceID");
      return m_instanceID;
  }
  
  }
  </code></pre>
  
  <p>I always though that web services were singleton by default.</p>
  
  <p>On the <strong>glassfish V2.1</strong> server the ws is working as expected, with a singleton lifecycle:</p>
  
  <ul>
  <li>Only one instance is created.</li>
  <li>Only one execution of constructor method.</li>
  <li>Only one execution if postconstructor method.</li>
  <li>Only one execution of predestroy method when ws is undeployed.</li>
  </ul>
  
  <p><em>Client log</em></p>
  
  <p>InstanceID = -4747957096764272596</p>
  
  <p>InstanceID = -4747957096764272596</p>
  
  <p>InstanceID = -4747957096764272596</p>
  
  <p><em>Server log</em></p>
  
  <p>26/08/2010 13:08:15:146 ***** first.test.FirstWS (m_instanceID=-4747957096764272596): Executing Constructor ThreadID=68 ThreadName=httpSSLWorkerThread-8080-0</p>
  
  <p>26/08/2010 13:08:15:161 ***** first.test.FirstWS (m_instanceID=-4747957096764272596): Executing PostConstruct ThreadID=68 ThreadName=httpSSLWorkerThread-8080-0</p>
  
  <p>26/08/2010 13:08:15:364 ***** first.test.FirstWS (m_instanceID=-4747957096764272596): Executing GetInstanceID ThreadID=69 ThreadName=httpSSLWorkerThread-8080-1</p>
  
  <p>26/08/2010 13:08:15:380 ***** first.test.FirstWS (m_instanceID=-4747957096764272596): Executing GetInstanceID ThreadID=69 ThreadName=httpSSLWorkerThread-8080-1</p>
  
  <p>26/08/2010 13:08:15:396 ***** first.test.FirstWS (m_instanceID=-4747957096764272596): Executing GetInstanceID ThreadID=69 ThreadName=httpSSLWorkerThread-8080-1</p>
  
  <p>26/08/2010 13:08:38:849 ***** first.test.FirstWS (m_instanceID=-4747957096764272596): Executing PreDestroy ThreadID=626 ThreadName=Thread-540</p>
  
  <p>But on the <strong>glassfish V3</strong> server the ws isn't working as expected, because a new instance of the ws is created for EVERY call to the GetInstanceID:</p>
  
  <ul>
  <li>One instance is created for every call.</li>
  <li>One execution of constructor method for every call.</li>
  <li>Two (!?!?!) executions of postconstructor method for every call.</li>
  <li>The predestroy method isn't called when ws is undeployed.</li>
  </ul>
  
  <p><em>Client log</em></p>
  
  <p>InstanceId = 7754248300017958713</p>
  
  <p>InstanceId = -1714184485890589231</p>
  
  <p>InstanceId = -4156829683887899017</p>
  
  <p><em>Server log</em></p>
  
  <p>INFO: 26/08/2010 15:16:11:429 ***** first.test.FirstWS (m_instanceID=7754248300017958713): Executing Constructor ThreadID=103 ThreadName=http-thread-pool-8080-(2)</p>
  
  <p>INFO: 26/08/2010 15:16:11:429 ***** first.test.FirstWS (m_instanceID=7754248300017958713): Executing PostConstruct ThreadID=103 ThreadName=http-thread-pool-8080-(2)</p>
  
  <p>INFO: 26/08/2010 15:16:11:429 ***** first.test.FirstWS (m_instanceID=7754248300017958713): Executing PostConstruct ThreadID=103 ThreadName=http-thread-pool-8080-(2)</p>
  
  <p>INFO: 26/08/2010 15:16:12:429 ***** first.test.FirstWS (m_instanceID=7754248300017958713): Executing GetInstanceID ThreadID=103 ThreadName=http-thread-pool-8080-(2)</p>
  
  <p>INFO: 26/08/2010 15:16:12:460 ***** first.test.FirstWS (m_instanceID=-1714184485890589231): Executing Constructor ThreadID=102 ThreadName=http-thread-pool-8080-(1)</p>
  
  <p>INFO: 26/08/2010 15:16:13:429 ***** first.test.FirstWS (m_instanceID=-1714184485890589231): Executing PostConstruct ThreadID=102 ThreadName=http-thread-pool-8080-(1)</p>
  
  <p>INFO: 26/08/2010 15:16:13:429 ***** first.test.FirstWS (m_instanceID=-1714184485890589231): Executing PostConstruct ThreadID=102 ThreadName=http-thread-pool-8080-(1)</p>
  
  <p>INFO: 26/08/2010 15:16:14:429 ***** first.test.FirstWS (m_instanceID=-1714184485890589231): Executing GetInstanceID ThreadID=102 ThreadName=http-thread-pool-8080-(1)</p>
  
  <p>INFO: 26/08/2010 15:16:14:445 ***** first.test.FirstWS (m_instanceID=-4156829683887899017): Executing Constructor ThreadID=103 ThreadName=http-thread-pool-8080-(2)</p>
  
  <p>INFO: 26/08/2010 15:16:15:429 ***** first.test.FirstWS (m_instanceID=-4156829683887899017): Executing PostConstruct ThreadID=103 ThreadName=http-thread-pool-8080-(2)</p>
  
  <p>INFO: 26/08/2010 15:16:15:429 ***** first.test.FirstWS (m_instanceID=-4156829683887899017): Executing PostConstruct ThreadID=103 ThreadName=http-thread-pool-8080-(2)</p>
  
  <p>INFO: 26/08/2010 15:16:15:429 ***** first.test.FirstWS (m_instanceID=-4156829683887899017): Executing GetInstanceID ThreadID=103 ThreadName=http-thread-pool-8080-(2)</p>
  
  <p>INFO: Closing Metro monitoring root: amx:pp=/mon/server-mon[server],type=WSEndpoint,name=/FirstWebApplication-FirstWSService-FirstWSPort</p>
  
  <p>So, why this behaviour on glassfish V3? How could I have a singleton web service on glassfish V3?</p>