2

I've started writing a java web service in NetBeans. But i'm opening and closing database connections in every single web method and that seems wrong. So are there any patterns for open/close database connections in java web service?

Web service like this;

@WebService(serviceName = "myWS ")
public class myWS {

 @WebMethod(operationName = "getName")
 public String getName(){
  //open connection
  //get data
  //close connection
 }

 @WebMethod(operationName = "getAge")
 public String getAge(){
  //open connection
  //get data
  //close connection
 }
}

And using Oracle JDBC. Thanks.

electron
  • 801
  • 8
  • 18
  • 3
    [Database connection pooling](https://docs.oracle.com/javase/tutorial/jdbc/basics/sqldatasources.html#pooled_connection) is what you need to do. – asgs Dec 09 '16 at 07:29
  • 1
    In addition to Conn pool that @asgs mentioned, I would advise against adding DB access code into Webservice classes. Use DAO classes to decouple your data access code from service code. Read more here: http://www.oracle.com/technetwork/java/dataaccessobject-138824.html and http://stackoverflow.com/questions/13785634/responsibilities-and-use-of-service-and-dao-layers – Pat Dec 09 '16 at 07:41

0 Answers0