views:

506

answers:

3

I'm working on a Java based project that has a client program which needs to connect to a MySQL database on a remote server. This was implemented is as follows:

Use JDBC to write the SQL queries to be executed which are then hosted as a servlet using Apache Tomcat and made accessible via XML-RPC. The client code uses XML-RPC to remotely execute these JDBC based functions. This allows us to keep our MySQL database non-public, restricts use to the pre-defined functions, and allows Tomcat to manage the database transactions (which I've been told is better than letting MySQL do it alone, but I really don't understand why). However, this approach requires a lot of boiler-plate code, and Tomcat is a huge memory hog on our server.

I'm looking for a better way to do this. One way I'm considering is to make the MySQL database publicly accessible, re-writing the JDBC based code as stored procedures, and restricting public use to these procedures only. The problem I see with this are that translating all the JDBC code to stored procedures will be difficult and time consuming. I'm also not too familiar with MySQL's permissions. Can one grant access to a stored procedure which performs select statements on a table, but also deny arbitrary select statements on that same table?

Any other ideas are welcome, as are thoughts and or sugguestions on the stored procedure solution.

Thank you!

+1  A: 

MySQL 5.0.3+ does have an execute privilege that you can set (without setting select privileges) that should allow you to get the functionality you seek.

However, note this mysql bug report with JDBC (well and a lot of other drivers).

When calling the [procedure] with JDBC, I get "java.sql.SQLException: Driver requires declaration of procedure to either contain a '\nbegin' or '\n' to follow argument declaration, or SELECT privilege on mysql.proc to parse column types."

the workaround is:

See "noAccessToProcedureBodies" in /J 5.0.3 for a somewhat hackish, non-JDBC compliant workaround.

Owen
+1  A: 

I am sure you could implement your solution without much boiler-plate, esp. using something like Spring's remoting. Also, how much memory is Tomcat eating? I frankly believe that if it's just doing what you are describing, it could work in less than 128mb (conservative guess).

Your alternative is the "correct by the book" way of solving the problem. I say build a prototype and see how it works. The major problems you could have are:

  • MySQL having some important gotcha in this regard
  • MySQL's Stored Procedure support being too primitive and forcing you to do a lot of work
  • Some other strange hiccup

I'm probably one of those MySQL haters, so the situation might be better than I think.

alex
Unfortunately, there is more than one book. The REST and the Semantic Web devotees would disagree with you (they'd see it as a missed opportunity), but your DBA will wonder why you had a web tier at all. The guy who maintains the client will also wonder why you're changing what you have!
Simon Gibbs
+1  A: 

You can probably get the RAM upgraded in your server for less than the cost of even a few days development time, so don't write any code if that's all you're getting from the exercise. Also, just because the memory is used inside of tomcat, it doesn't mean that tomcat itself is using it. The memory could be used up by data or by technical flaws in your code.

If you've tried additional RAM and it is being eaten up, then that smells like a coding issue, so I'd suggest using a profiler, or log data to try and work out what the root cause is before changing anything. If the cause is large data sets then using the database directly will only delay the inevitable, instead you'd need to look at things like paging, summarisation, client side caching, or redesigning clients to reduce the use of expensive queries. Using a profiler, or simply reviewing the code base, will also tell you if something is creating too many objects (especially strings, or XML nodes) or leaking memory.

Boiler plate code can be avoided by refactoring creatively, and its good that you do avoid repetition. Its unclear how much structure you might already have, but with a little work its easy to centralise boilerplate JDBCs calls. There is no fundamental reason JDBC code should be repeated, perhaps you could tell us what code is being repeated?

Finally, I'll venture that there are many good reasons to put a web tier over your database. Flexibility (of deployment), compatibility, control (over the SQL) and security are all good reasons to keep the web tier.

Simon Gibbs
Thanks for the accepted-answer endoresement. I recently used this tool: http://www.eclipse.org/mat/ to get an impression of memory usage. It has limitations but will certainly flag any big memory structures. Good luck.
Simon Gibbs