简体   繁体   中英

What is the proper way to handle login errors in Sybase, from a JDBC connection?

I access a Sybase database from a Java application. I can connect to it, execute statements, all of this works fine.

My issue is that I would like to handle correctly the cases when connection fails.

From my understanding, it can fail for the following reasons:

  • Incorrect password
  • Password expired
  • Account is locked

So my question is: how can I properly handle these error cases, how can I recognize which one happened?

From several tests, I found out the different cases:

Expired password

In this case, the database allows a connection, but a very limited one. The only thing you can call is the procedure to change password. The connection returned comes with a SQLWarning with the errorcode 4022 , and a description stating:

The password has expired, but you are still allowed to log in. You must change your password before you can continue. If a login trigger is set, it will not be executed.

Thanks to the specific error code, it is possible to recognized the error, and propose to change the password in the client program.


Invalid password and Locked account

There is no difference for both cases. When requesting a connection, it throws a SQLException which links to a SQLWarning as next exception, with the error code 4002 , and a very simple description:

Login failed.

As such, there is not really a way to handle these cases specifically.


Bonus case: password expiring soon

When a password will expire soon, the connection returned will contain a SQLWarning with the code 4023 , stating:

Your password will expire in %s days.

This allows to show a warning in the client program, proposing the change the password already.

Handle it like you would other messages from the database. Catch the exception a log the message (this will help you identify the cause. At the same time display a message to the user that either a database error occurred or that the database could not be accessed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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