質問

I am developing Android application which Gets and Posts data to database through REST service. I have configures REST service with Jersey and Tomcat in Java.

I created single database connection when REST service starts.Now all the android clients using the single database connection.Now i just want to know is it good practice to use single database connection in my scenario..Can single database connection can entertain more than one concurrent requests ??

Kindly suggest....

Updated When i test following scenario ,it gives different results..i just configured simple REST server with tomcat using Jersey with Get annotation only, inside this annotation i write the code to get the name of employee whose id is 300 using database connection which was established when REST was started..so that when i write http://192.168.15.9:8080/Rest/rest/person/sample on my web browser i got employee name...Then i created batch file to open the url in 100 tabs, i executed the batch file. At same time i open the url from my virtual machine and got the employee name with no delay..Also employee name is displayed in 100 tabs after 20 seconds..If single database connection can not be used for concurrent users then why i gives response rapidly ?? Can any one please explain ??

役に立ちましたか?

解決

No, a single database connection should not be used for concurrent requests. You should create a connection pool (basically a set of reusable connections), take a connection from the pool as needed, and then release it to the pool.

Don't do it yourself!

Since you have Tomcat, you can configure a JDBC data source and then use JNDI to get a reference to this data source. A data source will provide you with connections, without you having to create connections by yourself. Tomcat will pool (activate, maintain and dispose) connections under the hood.
Alternatively, you can use a custom connection pooler like c3p0, but I strongly advice you to use the facilities provided by your server.

If, for any reason, you have to deploy your product within a few minutes (so you have no time to set-up a data source), then you better open and close connections for every request - which is wasteful - rather than sharing a single connection across all requests.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top