如何在REST服务启动时连接MySql数据库?(How to Connect MySql Database on REST service start-up?)

在Apache Tomcat 7和Jersey Jars中使用REST服务。

我正在开发一个android应用程序,它通过java中的REST服务获取和发布服务器上的一些数据。对于来自android客户端的每个请求,REST服务器打开数据库连接,执行查询,返回结果并关闭数据库连接。

public String postPerson( MultivaluedMap<String, String> personParams ) { EmployeeData empD=new EmployeeData(); //Connecting with database on empty constructor int id = personParams.getFirst(EMPLOYEE_ID); String employeeDepartment=empD.getDepartment(id); //Getting department name empD.disconnectDb(); return(empD); }

上面的场景工作正常,但没有有效的响应时间。 现在我想在REST服务启动时创建数据库连接,以减少对android客户端的响应时间..我怎样才能实现这一点?

Using REST service with Apache Tomcat 7 and Jersey Jars.

I am developing a android application which gets and post some data on server through REST service in java.For each request from android client the REST server Opens database connection, executes query, returns the result and closes database connection..

public String postPerson( MultivaluedMap<String, String> personParams ) { EmployeeData empD=new EmployeeData(); //Connecting with database on empty constructor int id = personParams.getFirst(EMPLOYEE_ID); String employeeDepartment=empD.getDepartment(id); //Getting department name empD.disconnectDb(); return(empD); }

The above scenario is working fine but not efficient response time. Now i want to create database connection on REST Service start-up to decrease the response time to android client.. How can i achieve this ????

最满意答案

查看以下链接。 我希望这些链接可以解决您的问题,但请记住,永远不要在同一个数据库连接上执行并发事务。 通过遵循该过程,您还可以在服务启动时建立与星号管理界面的连接。

http://www.programcreek.com/2009/07/put-database-connection-to-servletcontextlistener/

http://docs.oracle.com/cd/B15904_01/web.1012/b14017/filters.htm#i1000654

Check out the following links. I hope that those links will solve your problem but remember that never perform the concurrent transaction over the same database connection. By following the procedure you will be also able to establish connection to asterisk management interface at service start up.

http://www.programcreek.com/2009/07/put-database-connection-to-servletcontextlistener/

http://docs.oracle.com/cd/B15904_01/web.1012/b14017/filters.htm#i1000654

更多推荐