4

I read somewhere use of webservcies in apps. After a lot of research I am able to create one Webservice which will accept Json and JsonP both format as request and response accordingly. I developed the webservcies using Java, Apache Axis2, Hibernate and MySQL as database. there are few problems and I dont know how to solve ?

  1. Insert or delete option, sometimes if at a time more than two users call that service that is insert or delete any row the queries go in sleep mode and next time someone tries to fetch that service he couldnt. Accroding to server log it says error SQL Lockout State. If I checks Processlist in MYSQL it is showing that query in Sleep, I have to kill to resume.

  2. The performance of webservice doesnt seems to be upto mark, it takes time some more time as what i experienced it shouldn't. In simple words how to obtain better performance by the services

  3. How to implement security feature such that if a user logins he/she can be provided an id and validation of that id so that unauthorized access can be prevented

Or just guide me what should be the most appropriate and optmized Webservice methodology that can be used using Java

ajitksharma
  • 4,523
  • 2
  • 21
  • 40

5 Answers5

2

Answer to this question is not specific to Android. Below are my investigations which might be useful for you.

For the point about MySQL connections going to sleep mode, you can do the following.

  • Debug the datasource used by Hibernate, try to increase the pool size & check for any issues in it.
  • Define a timeout period for connections. JBoss has several configurations related to this like blocking-timeout-millis, idle-timeout-minutes etc.
  • Declare a mechanism to validate periodically the connection resources in the pool for activeness. You can explore OracleStaleConnectionChecker for options.
  • Configure miniumn connections in the pool. This is important because when all the stale connections are discarded, empty pool needs to be pre-filled & ready with active connections.

Coming to performance of Insert/Delete operations & SQL Lockout State, please try to re-order the sequence of the queries which you are firing to DB at every request. This may not be a deadlock situation but sequencing DB queries correctly will definitely lead to less lockout time and better performance.

This answer may be of use for you. Hibernate: Deadlock found when trying to obtain lock

Web-services which you have developed may require some performance optimization to make them upto the mark. Below are first few steps you can take to bring the performance up.

  • Avoid nested loops. Every extra parameter in the iterated lust increase the order of the lopp exponentially.
  • Remove early initialization of objects. This may lead to long unwanted GC cycles.

Apart from above optimizations, there are several frameworks & tools at your service to evaluate the code quality & its performance. PMD, FindBugs, JMeter, Java profiler are few of them to name.

Shishir

Community
  • 1
  • 1
Shishir Kumar
  • 7,981
  • 3
  • 29
  • 45
1

You are going to have to profile your server and see where the time is spent. I really like YourKit for doing thread profile. visualvm which comes with the JDK can help also.

There are all sorts of reasons your web service can be slow:

  • Latency from client to server
  • Handling the HTTP request on the server
  • Handling the HTTP response on the client
  • Making the database call (sounds like you already have some kind of locking / blocking going on there)

You are going to have to get markers to tell you how long it took to go from A to B to C to D back to C back to B back to A kind of thing. We would be speculating heavily from here on what is exactly going on in your program, but we can give you the ideas / tools to figure it out.

If you use YourKit, connect it to your server process. Have nothing else connecting to your server (for instance your client is not sending requests). Try it with your client requesting, you should see your accepting threads receive the HTTP request and then delegate to either your processing thread or do the processing itself. You can use YourKit to see how much time is spent in different functions during that call time.

Try it with your client making the call. Try it using a simple HTTP request tool like wget or maybe your IDE has a webservice test tool (for instance intellij does), or you can download a simple HTTP test tool.

By testing it in a simple tool that just outputs the response, you can eliminate any client processing issues. You can also achieve a similar test in Chrome or Firefox and use the developer tools to see time to fulfill request.

In my experience, the framework for handling the requests and delegating can introduce some performance issues. I ripped Grails out of a production environment because of its performance issues (before any Grails / Groovy flames come my way, we were operating at a much higher rate than typical web applications, and I am sure Grails has made some headway in the last couple years... alas, it was not for my need at that time)

BTW, I doubt you are operating a load where you will be critiquing the web service framework you chose to use. I have been happy with Spring MVC and DropWizard (Jersey JAX-RS), and Grails is easy to use too.

You should make a simple static content response in your webservice and see how quickly that returns vs a request that makes a database call.

Also, what kind of table are you using in MySQL? InnoDB? MyISAM? They have different locking schemes. That could be causing your MySQL issue.

The key to all of it, break the problem up into parts, and measure each and eliminate parts one by one till you go, everytime I do X it is slower (like everytime I make a database call its slower)

mhoglan
  • 371
  • 2
  • 6
0

In Java the the way you will be able to find more support online via documentation/forums is to develop the web service as a REST web service using Spring MVC.

You can base yourself on this resource and take it from there:

Angular University
  • 42,341
  • 15
  • 74
  • 81
  • I already have created the webservice, the problem I am trying to explain is performance related and spmetimes database lockout issues when at same moment two or more users hitting webservice – ajitksharma Feb 21 '14 at 05:15
  • this was about the last part of the question on most appropriate and optmized Webservice methodology. for the performance problem could you post some code/configuration, namelly the configuration of the datasource and an example of a modification transaction – Angular University Feb 21 '14 at 08:26
0

Using Spring you can create a RestFul webservice easily and spring does all the ground work you needed. As others had mentioned you can consume the webservice in any type of client - including Android.

A detailed guide available here: https://spring.io/guides/gs/rest-service/

18bytes
  • 5,951
  • 7
  • 42
  • 69
0

Here are my suggestions:

  1. Make APIs only read or write database. If an API combines reading and writing, it is possible to cause deadlock;
  2. Use a light-weight HTTP server. Powerful HTTP server is possibly consuming more.
  3. Make use of thread. Have more threads could be helpful when you are facing a ton of users.
  4. Make more things static. You could avoid unnecessary queries.

I think mhoglan's answer is detailed enough.

CSakura
  • 538
  • 1
  • 6
  • 17