简体   繁体   English

使用SQLException检查Java程序中是否存在MySQL表

[英]Checking wether a MySQL table exists in a Java program using SQLException

I have a Java Web Application where my clients connect to the Web Service to update a MySQL database (using JDBC) with their data. 我有一个Java Web应用程序,我的客户端连接到Web服务以使用其数据更新MySQL数据库(使用JDBC)。 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. 我想要做的是每次客户端连接并使用Web Service的操作时,是否不使用MetaData来检查请求的表是否存在。 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 (eg check that it will refresh its entry in the database every x seconds). 我想到了每次都执行INSERT并且连接到Web服务的第一个客户端将导致我将捕获的SQLException ,查看其ErrorCode以确定它是否由不存在的表引起,如果是这种情况,它将(在catch子句),创建表,插入由于Exception而无法早些插入的数据,然后创建一个线程来处理该特定客户端(例如,检查是否每隔x秒刷新一次其在数据库中的条目)。 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? 即使线程将在Exception的catch子句中启动,该线程也将正常运行吗?

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. 按照描述的方式进行操作会将您的应用程序与MySQL绑定在一起,因为返回的异常必须作为MySQL特定的对象进行检查,以确定它们是否由丢失的表而不是其他问题引起。 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. 此外,如果您一次收到2个针对同一不存在表的请求,则很可能会导致2个线程尝试创建同一表,而其中一个线程在尝试创建已存在的表时会出错。需要进行某种同步。

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. 如果绝对必须动态创建它们,那么我将创建一个单例对象,该对象使用MetaData确定每个表的存在,然后将该知识缓存在对象本身中,以便您仅需要在每个网络生命周期中搜索每个表的存在一次。应用。 You could also synchronize the methods on this object to prevent 2 threads from attempting to create the same table at once. 您还可以同步此对象上的方法,以防止2个线程尝试一次创建同一表。

Using Exceptions as a mean of flow control is an anti pattern, you should avoid doing it. 使用异常作为流控制的手段是一种反模式,您应避免这样做。 If you are worried about the performance implications of querying for meta data for each connection, you could cache the results. 如果您担心查询每个连接的元数据会对性能产生影响,则可以缓存结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM