0

I want to implement the following interface on a 2-Tier application with an MS SQL-Server 2008R2 (i.e. no app server in between)

interface ILicense {
  void Acquire(string license);
  void Release(string license);
}

However, I want to release the license even if the application is killed or bombs out without calling the Release method. I also want to avoid using a timer which refreshes the license every minute or so.

So I thought: Use a dedicated SqlConnection together with the sp_getapplock and sp_releaseapplock SP because that's what they are seemed to be made for. Now I found out that the SP only work from within a transaction, so I would need to keep the transaction open all the time (i.e. while the application is running). Anyway, it works that way. The application starts, opens the connection, starts the transaction, and locks the license. When the application terminates, the connection is closed, everything is rolled back and the license is released. Super. Whenever the running app needs to switch licenses (e.g. for another module), it calls Release on the old license and then Acquire on the new one. Cool.

Now to my question(s):

  1. Is it acceptable to have an open (uncommitted) transaction open on a separate connection for a long time?
  2. Are there any better possibilities to implement such a 'lock' mechanism? The problem is that the license shall be released even if the application terminates unexpectedly. I thought of some sort of 'logout' trigger, but that does not exist in SQL-Server 2008R2
4
  • Are you implementing a license server? Is server physically accessible to end-users? As-is it may be a waste of resources but unless you have hundreds simultaneous open connections you may not worry (keep in mind that maximum number of user connections is fixed and it depends on the SQL Server version). However from licensing point of view it's a pretty weak. Malicious user can restore a backup of your master DB, get hashed password, login with hashed password and change server code (or just manually release the lock in your working server, eventually inside a scheduled Job...) Jun 29, 2016 at 10:18
  • @AdrianoRepetti: Well, yes. Pessimistic locking and a license server. Basically, I need mutexes.
    – alzaimar
    Jun 29, 2016 at 11:20
  • IMO (but note I'm free-talking without much context) it works ("go horse...") and wasted resources (I read a nice article about long running queries, working threads and connections but I can't find it now) is limited for a fairly low number of users. What I am more concerned about is strongness of this server. If users have physical access to that machine then protection is easy to circumvent (and if you're planning to do it over internet...well...don't). What's bad on a periodic "ping" to license server? It uses much less resources, it's fast (and it may be embedded in queries) Jun 29, 2016 at 11:26
  • The current implementation of the lock uses a table on the server and a timer which periodically updates a time stamp ('License valid until'). A new attempt to lock a license looks for outdated entries and reserves them. There is nothing wrong with this except that I thought it would be nicer to avoid polling, which I don't like as a pattern. If there is a better solution in terms of less complexity, less overhead, I would rather use it. But it seems as if the timer/polling attempt is an accepted and simple solution.
    – alzaimar
    Jun 30, 2016 at 10:07

1 Answer 1

0

I am by no means the SQL or DB guru that some of the members of this site are but your setup brings up a few concerns or things to consider.

  1. this could really limit the number of concurrent users that your application could have especially in a 2-tier architecture. Now in a 3 tier approach the app server would manage and pool these connections/transactions but then you would lose the ability to use those stored procs to implement your licensing mechanism, i believe.

  2. with the transaction being open for some indeterminate period of time I would worry about the possibility of the tempdb growing too big or exceeding the space allocated to it. i don't know what is going on in the app and if there is anything else going on in that transaction, my guess is no but thought i would mention it.

  3. I hope i am not getting my SQL versions mixed up here but transaction wraparound could cause the db to shutdown.

  4. This limits your app significantly as the data in the transaction has a lock on it that won't be released until you commot or rollback.

There must be a more elegant way to implement a licensing model that doesn't rely on leaving a transaction open for the life of the app or app module. If you have a two tier app then that implies that the client always has some kind of connectivity so maybe generate some kind of unique id for the client and either add a call home method or if you really are set on there being an instantaneous verification then everytime the client performs an action that queries the db have it check to see if the client is properly licensed etc.

Lastly, in all of the SQL teachings I have received from other db guys who actually really know there stuff this kind of setup (long running open transaction) were never recommended unless there was a very specific need that could not be solved otherwise.

1
  • Thanks for the answer. I know that transactions should be kept as short as possible. The reason for this is to minimize the time a record/table is locked. However, I don't know the implications on e.g. tempdb or other resources, hence my question.
    – alzaimar
    Jun 29, 2016 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.