简体   繁体   English

在Java中创建自定义SQL检查的异常?

[英]Creating a custom SQL checked exception in java?

I know how to create a basic custom checked exception, but I want to be able to create an exception that checks the following: 我知道如何创建一个基本的自定义检查异常,但是我希望能够创建一个检查以下各项的异常:

  1. I basically have some tables in a DB, in that DB I have a table message with 2 columns message and queue. 我基本上在数据库中有一些表,在该数据库中,我有一个带有两列消息和队列的表消息 I can only enter a message with a queue number,if that queue number exists in table queue_table (ie foreign key concept). 如果该队列号存在于表queue_table中 (即外键概念),则只能输入带有队列号的消息。 I want to be able to check, that if a person retreives topmost message from a queue (in table message),(ie retreives topmost row with a particular queue number) that there exists that queue number in the queue_table table. 我希望能够检查一下,如果某人从队列中(表消息中)检索最上面的消息(即,检索具有特定队列号的最上面的行),则queue_table表中存在该队列号。 If it doesnt I want it to trigger an exception called QueueDoesNotExistException. 如果不是,我希望它触发一个称为QueueDoesNotExistException的异常。 Without the exception, the query to obtain messages from a non existing queue in queue_table gives me null , so I am not sure if an existing exception will be useful. 没有异常,从queue_table中不存在的队列中获取消息的查询给了我null ,因此我不确定是否存在一个有用的异常。

So I have the following : 所以我有以下内容:

package asl.exceptions;

public class QueueDoesNotExistException extends Exception {
    public QueueDoesNotExistException(){



    }
}

I am not sure how to handle this, I dint find any resources for this, or am I looking at the wrong place? 我不确定如何处理此问题,我没有找到任何相关资源,还是我看错地方了?

I read this : https://docs.oracle.com/javase/tutorial/jdbc/basics/sqlexception.html 我读了这个: https : //docs.oracle.com/javase/tutorial/jdbc/basics/sqlexception.html

But dint find it useful. 但是,发现它很有用。

you need to catch DB-specific exception thrown by your JDBC driver first. 您需要首先捕获JDBC驱动程序引发的特定于数据库的异常。 then you can wrap it in your exception and propagate further. 那么您可以将其包装在异常中并进一步传播。 but exception-driven logic is rarely a good design. 但是异常驱动逻辑很少是一个好的设计。

You should never let the flow get to the point where a customer is trying to access a message from a non-existent queue. 您永远不应让流程到达客户试图从不存在的队列访问消息的地步。 You check for queue existence first thing in the workflow and abort the workflow with an exception, without ever making the second query for messages in the queue. 您首先检查工作流中是否存在队列,然后异常中止工作流,而无需对队列中的消息进行第二次查询。

Based on comments, it seems you are trying to validate the query itself , as opposed to ensuring that it cannot return invalid data. 基于注释,似乎您正在尝试验证查询本身 ,而不是确保查询不能返回无效数据。 Apparently, you also want to distinguish between a query that returns zero rows on account of the specified queue not being recorded in the DB, and a query that returns zero rows because there are simply no messages available in that queue. 显然,您还希望区分由于未在数据库中记录指定队列而返回零行的查询与返回零行的查询,因为该队列中根本没有消息可用。 I'm doubtful of the usefulness of these pursuits, but if you insist, then here's an approach you could take: 我怀疑这些追求是否有用,但是如果您坚持要这样做,那么可以采取以下方法:

  1. Ensure that your Connection is not in auto-commit mode ( connection.setAutoCommit(false) ) 确保您的Connection未处于自动提交模式( connection.setAutoCommit(false)
  2. Attempt to select the specified queue from the queue_table by its ID. 尝试从queue_table通过其ID选择指定的队列。 If that fails, then roll back the connection's transaction and throw the exception of your choice. 如果失败,则回滚连接的事务并抛出您选择的异常。
  3. Otherwise, perform the main query as ordinarily you would do, and unless it throws, commit the transaction. 否则,请按通常的方式执行主查询,除非引发该查询,否则请提交事务。
  4. Be watchful for the query returning zero rows, and handle the situation however is appropriate. 注意查询返回零行,但是处理这种情况是适当的。
  5. If you rely on changing the autocommit mode as part of the procedure, be sure to record and restore the original autocommit mode, including in the event of an exception. 如果您在此过程中依赖更改自动提交模式,请确保记录并恢复原始的自动提交模式,包括发生异常的情况。

I apologize if that seems simple or nonmagical, or whatever. 如果这看起来简单或不可思议,则我深表歉意。

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

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