0

I am new Java web service programmer and I am looking to see why I see 2 instances of my service being created when I have 2 web pages open to it (One over localhost and another from a different PC using the server PC's IP address and web service URL).

Stack details: Using Tomcat, Java web service under Windows 7 PC.

I have a small web service that connects to a black box internally. Each time I bring up the web service and open up a URL associated with it, I can see the details from the black box. The problem is, when I have 2 web pages open to the web service (One over localhost and another from a different PC using the server PC's IP address and web service URL), I see two instances of the service being created and that are trying to connect to one black box at the same time. I need the ability to start the connect to the black box remotely or locally. But, only the localhost connection should win. I tried creating a servlet to start the service when Tomcat startup but not much luck with it. With the servlet, I see my autowired Datasource connection is null.

Any idea on this would help.

@Repository public class HServiceDAO {

@Autowired private DataSource mySql_dataSrc; 

web.xml

<servlet> <servlet-name>StartWebApp</servlet-name> <servlet-class>com.StartWebApp</servlet-class> <load-on-startup>1</load-on-startup> </servlet> 
4
  • Did you create the service as a Singleton? Also be aware that servlets are not singleton per se: stackoverflow.com/questions/11820840/is-servlet-the-singleton Commented Sep 29, 2015 at 6:55
  • Will try to create it as singleton. As per this post, the service runs as a Singleton, is it? stackoverflow.com/questions/11096310/… Commented Sep 30, 2015 at 15:15
  • Yes you can use a static variable, or let Spring do the work for you. Commented Sep 30, 2015 at 19:21
  • I am trying to have Spring do the work using @Autowired Commented Oct 2, 2015 at 16:50

1 Answer 1

0

Created a singleton class for the service that started the connection to the black box. This eliminated the duplication connection to the black box.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.