Limiting Java Application Access to the Database

时间:2016-12-09 12:46:16

标签: java sql-server jdbc

I am creating a java application that stores its data in an SQL Server Database. The application will be installed in different machines all of which will be networked using the normal Local Area Network to be able to access the database server through the network. I have already created my connection class which works perfectly.

I would like to limit the number of users who can be accessing the database through the application at any given time. For instance, if I limit the number of users to a max of 10 users, an 11th user will get a message notifying that the maximum number of users are logged in and when one user logs out (meaning that 9 users remain logged in) the 11th (who now will be the 10th) user can be able to login.

How can I be able to achieve this using java?

2 个答案:

答案 0 :(得分:0)

Looking online I found that SQL server allows you to set up a maximum number of connections.

That will easily allow you to limit the number of connection to the DB. After that, you have just to manage the refused connection, probably catching an exception ad propagating the error somehow till your ui.

Reference: https://msdn.microsoft.com/en-us/library/ms187030.aspx (this applies to SQL server 2016. For other version, just search for 'server configuration option sql server ' )

答案 1 :(得分:0)

You could create a new table that contains 'session identifiers'. So for each client that is logged in there is one record.

Then create a stored procedure that starts a transaction. Inside the transaction check if the maximum number of session records is reached or not. If so, return something that indicates that the maximum is reached. If not, generate a new session identifier, store it in the table and return it. This will be the 'log in' function.

Finally you will need another stored procedure that deletes a session record. This will be the 'log out' function.