<p>In your applications, what's a "long time" to keep a transaction open before committing or rolling back?  Minutes?  Seconds?  Hours?</p>

<p>and on which database?</p>

[{'answer_id': 10730, 'author': 'DevelopingChris', 'author_id': 1220, 'author_profile': 'https://Stackoverflow.com/users/1220', 'pm_score': 3, 'selected': False, 'text': "<p>transactions: minutes.</p>\n\n<p>Cursors: 0seconds maximum, if you use a cursor we fire you. </p>\n\n<p><strong>This is not ridiculous when you consider we are in a high availability web environment, that has to run sql server, and we don't even allow stored procs because of inability to accurately version and maintain them. If we were using oracle maybe.</strong></p>\n"}, {'answer_id': 10732, 'author': 'lomaxx', 'author_id': 493, 'author_profile': 'https://Stackoverflow.com/users/493', 'pm_score': 3, 'selected': False, 'text': "<p>I'm probably going to get flamed for this, but you really should try and avoid using cursors as they incur a serious performance hit.  If you must use it, you should keep it open the absolute minimum amount of time possible so that you free up the resources being blocked by the cursor ASAP.</p>\n"}, {'answer_id': 10785, 'author': 'BlaM', 'author_id': 999, 'author_profile': 'https://Stackoverflow.com/users/999', 'pm_score': 2, 'selected': False, 'text': '<p>Generally I agree with the other answers: Avoid cursors when possible (in most cases) and close them as fast as possible.</p>\n\n<p>However: It all depends on the environment you\'re working in. </p>\n\n<ul>\n<li>If it is a production website environment with lots of users, make sure that the cursor goes away before someone gets a timeout. </li>\n<li>If you\'re - for example - writing a "log analyzing stored procedure" (or whatever) on a proprietary machine that does nothing else: feel free to do whatever you want to do. You\'ll be the only person who has to wait. It\'s not as if the database server is going to die because you use cursors. You should consider, though, that maybe usage behaviour will change over time and at some point there might be 10 people using that application. So try to find another way ;)</li>\n</ul>\n'}, {'answer_id': 10818, 'author': 'ninesided', 'author_id': 1030, 'author_profile': 'https://Stackoverflow.com/users/1030', 'pm_score': 3, 'selected': True, 'text': '<p>@lomaxx, @ChanChan: to the best of my knowledge cursors are only a problem on SQL Server and Sybase (T-SQL variants). If your database of choice is Oracle, then cursors are your friend. I\'ve seen a number of cases where the use of cursors has actually improved performance. Cursors are an incredibly useful mechanism and tbh, saying things like "if you use a cursor we fire you" is a little ridiculous.</p>\n\n<p>Having said that, you only want to keep a cursor open for the absolute minimum that is required. Specifying a maximum time would be arbitrary and pointless without understanding the problem domain.</p>\n'}, {'answer_id': 10973, 'author': 'lomaxx', 'author_id': 493, 'author_profile': 'https://Stackoverflow.com/users/493', 'pm_score': 2, 'selected': False, 'text': "<p>@ninesided: performance issues aside, it's also about using the right tool for the job. Given the choice to move the cursor out of your query into code, I would think 99 times out of 100 it would be better to put that looping logic into some sort of managed code. Doing so allows you to get the advantages of using a debugger, compile time error checking, type saftey etc.</p>\n\n<p>My answer to the question is still the same, if you're using a cursor, close it ASAP, in oracle I'd also be trying to use explicit cursors.</p>\n"}]
['https://Stackoverflow.com/questions/10727', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/116/']