I have a Java Web Application where my clients connect to the Web Service to update a MySQL database (using JDBC) with their data. What i want to do is to check wether the requested table exists without using MetaData every time a client connects and uses an operation of the Web Service. I thought of just executing INSERT
every time and the first client that connects to the web service will cause an SQLException
that i will catch, look at its ErrorCode
to decide wether it is caused by nonexistant table and if that's the case it will (inside the catch clause) create the table, insert the data that couldn't be inserted earlier because of the Exception and then create a thread to handle that particular client (e.g. check that it will refresh its entry in the database every x seconds). Will this implementation do the job? Is the thread going to run properly and everything, even though it will be started inside the catch clause of an Exception?
Doing it the way you describe will tie your application to MySQL, since the exceptions that come back will have to be examined as MySQL specific to determine if they are caused by a missing table and not some other problem. Most developers consider it bad practice to tie an application to a specific database like this.
Also, if you get 2 requests for the same non-existent table at once, you could very likely get 2 threads attempting to create the same table and one of them will get an error trying to create a table that already exists, so you would need to do some kind of synchronization.
I suggest that you re-examine your application design so that all tables exist in advance. If they absolutely must be created dynamically, I would create a singleton object that used MetaData to determine existence of each table and then cache that knowledge in the object itself so that you only need to search for existence of each table once per life of the web application. You could also synchronize the methods on this object to prevent 2 threads from attempting to create the same table at once.