Home » SQL & PL/SQL » SQL & PL/SQL » Select statement is blocking a delete statement (merged 2) (Oracle, 10.2.0.4.0, Red Hat Enterprise Linux AS release 4 (Nahant Update 5))
Select statement is blocking a delete statement (merged 2) [message #539054] Wed, 11 January 2012 03:16 Go to next message
archana485
Messages: 22
Registered: June 2010
Junior Member
Hi,

I am using JDBC to run a few queries from my Java program (multi-threaded one).

I am facing an issue where a select statement is blocking a delete statement. From the java code point of view, there are 2 different threads accessing the same tables (whith different DB connection objects).

When the block occurs (which i was able to find out from the java thread dump that there is a lock on oracle), the below is the output:


SQL> SELECT TO_CHAR(sysdate, 'DD-MON-YYYY HH24:MI:SS')
  2  || ' User '||s1.username || '@' || s1.machine
  3  || ' ( SID= ' || s1.sid || ' ) with the statement: ' || sqlt2.sql_text
||' is blocking the SQL statement on '|| s2.username || '@'
  4    5  || s2.machine || ' ( SID=' || s2.sid || ' ) blocked SQL -> '
  6  ||sqlt1.sql_text AS blocking_status FROM v$lock l1, v$session s1, v$lock l2 ,
  7  v$session s2,v$sql sqlt1, v$sql sqlt2
  8  WHERE s1.sid =l1.sid
  9  AND s2.sid =l2.sid AND sqlt1.sql_id= s2.sql_id 
AND sqlt2.sql_id= s1.prev_sql_id AND l1.BLOCK =1
10  AND l2.request > 0 AND l1.id1 = l2.id1 AND l2.id2 = l2.id2;

BLOCKING_STATUS
--------------------------------------------------------------------------------
10-JAN-2012 08:53:51 User LTERTIO@TERTIO-SRV ( SID= 138 ) with the statement: SE
LECT a.ID,a.FILENAME FROM OTABATCHFILETABLE a , OTABATCHEPTTABLE b WHERE a.ID =
b.FILE_ID AND a.STATE IN ('P','C','E') AND b.LINK_ID='SmarttrustDP71_link1' is b
locking the SQL statement on LTERTIO@TERTIO-SRV ( SID=187 ) blocked SQL -> DELET
E FROM OTABATCHEPTTABLE WHERE EPT_ID=0001569738120000001



From the above it can be seen that a select statement is blocking a delete. Unless the select is select for Update, it should not block other statements is not it ? Please correct me if i am wrong and point out what the problem might be and if any more information is required.

Thanks,
Archana

[Updated on: Wed, 11 January 2012 04:49] by Moderator

Report message to a moderator

Re: Select statement is blocking a delete statement (merged 2) [message #539056 is a reply to message #539054] Wed, 11 January 2012 03:22 Go to previous messageGo to next message
cookiemonster
Messages: 13975
Registered: September 2008
Location: Rainy Manchester
Senior Member
No selects do not block deletes unless they are select for update. I suspect your query is showing the statement the blocking session is currently running, as opposed to the statement that caused the lock. Remember the blocker can go on and do other things after taking the lock.
Re: Select statement is blocking a delete statement (merged 2) [message #539059 is a reply to message #539056] Wed, 11 January 2012 03:35 Go to previous messageGo to next message
archana485
Messages: 22
Registered: June 2010
Junior Member
Hi,

Thanks for confirming. But if the above query is the one that is currently running, then at some point it has to release the lock on the table for other statements to execute right ?

I am asking this as because of this reason, my program has got hung and stops processing further unless i kill the process and re-start. Is there some way to handle this blocking ?

Also, I have been going through some other posts here and there are some bugs reported and fixed with blocking sessions- I am not sure if this is applicable to my scenario. Can you point me to some site which has a list of patches or bug fixes for particular oracle release ?
Re: Select statement is blocking a delete statement (merged 2) [message #539061 is a reply to message #539059] Wed, 11 January 2012 03:39 Go to previous messageGo to next message
cookiemonster
Messages: 13975
Registered: September 2008
Location: Rainy Manchester
Senior Member
I see no reason to assume this is an oracle bug, it is almost certainly a design flaw in the code.
The blocking session will hold the lock until it issues a commit or rollback. So you need to trace the code the blocking session is executing, work out what statements it issues to take the lock and what it does between that and the next commit/rollback.
Re: Select statement is blocking a delete statement (merged 2) [message #539062 is a reply to message #539059] Wed, 11 January 2012 03:39 Go to previous messageGo to next message
Michel Cadot
Messages: 68774
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Quote:
Thanks for confirming. But if the above query is the one that is currently running, then at some point it has to release the lock on the table for other statements to execute right ?

Wrong, the select does not hold any lock, the previous transaction statements may hold some and the lock are release when the transaction ends.

Can you point us to bugs related to blocked sessions issue?

Regards
Michel
Re: Select statement is blocking a delete statement (merged 2) [message #539064 is a reply to message #539062] Wed, 11 January 2012 03:50 Go to previous messageGo to next message
archana485
Messages: 22
Registered: June 2010
Junior Member
"So you need to trace the code the blocking session is executing, work out what statements it issues to take the lock and what it does between that and the next commit/rollback."


Actually in my code, it establishes a simple JDBC connection and executes the below queries( One thing to note here is that these queries might be issued parallely because they are being invoked by 2 different threads). And in the Java code we have set oracle to commit automatically once the queries are executed (autocommit=true)

SELECT a.ID,a.FILENAME 
FROM OTABATCHFILETABLE a , OTABATCHEPTTABLE b 
WHERE a.ID =b.FILE_ID 
AND a.STATE IN ('P','C','E') 
AND b.LINK_ID='SmarttrustDP71_link1'

DELETE FROM OTABATCHEPTTABLE 
WHERE EPT_ID=0001569738120000001


"Wrong, the select does not hold any lock, the previous transaction statements may hold some and the lock are release when the transaction ends."



So how can we find out what transaction is blocking the statement, also why is the lock never released ?

The below is one discussion which spoke about a similar problem (though in that case a select was blocking another select)

https://forums.oracle.com/forums/thread.jspa?threadID=941890

Again, like i mentioned earlier, i am not sure if this is applicable in my case- was just speaking my assumptions aloud.

Thanks,
Archana

[Updated on: Wed, 11 January 2012 04:49] by Moderator

Report message to a moderator

Re: Select statement is blocking a delete statement (merged 2) [message #539072 is a reply to message #539064] Wed, 11 January 2012 04:08 Go to previous messageGo to next message
cookiemonster
Messages: 13975
Registered: September 2008
Location: Rainy Manchester
Senior Member
As for that link: The OP is making the same mistake you made - assuming that the query shows the sql that caused the block. It doesn't. In fact it shows the previous sql statement run by the blocking session, not the current one.
I'm not at all convinced that final post has anything to do with the problem.

Is the only code against OTABATCHEPTTABLE run as individual statements from the java followed by a commit? Or are there more complicated calls? stored procedures maybe?
That select will not take a lock on the table.
Maybe you should trace the sessions in the DB and see if the java is actually commiting when you think it is.
Re: Select statement is blocking a delete statement (merged 2) [message #539077 is a reply to message #539072] Wed, 11 January 2012 04:25 Go to previous messageGo to next message
archana485
Messages: 22
Registered: June 2010
Junior Member
There are totally 3 tables which the program accesses/updates.

There are no stored procedures used and there are a lot of other queries (mainly most of them selects- only 1 insert and 2 or 3 updates and 1 delete)
but at the place where the code has hung the select statement is run twice with different parameters in the IN clause.
Also, one point that i mentioned previously was not correct, the code is doing an explicit commit after executing the statement (just realised that for selects you did'nt do commits and checked for the method that did a delete ! Embarassed )


 try {
			 connection = dbPool.getAvailableDBConnection();
			 if(connection!=null){
			 String query="DELETE FROM " + get_batchEptTable() + " WHERE EPT_ID=" + eptId;
			 smt=connection.createStatement();
			 result = smt.executeUpdate(query);
			 if(result > 0)				
				 connection.commit();
			 }
		 } catch (SQLException e) {			 
			 connection.rollback();
			 Tome.trace("DbConnection::deleteEpt", Tome.MW_VERBOSE_TRACE,
			 "An SQL Exception occured while deleting records");
			 throw e;
}



Can you please tell me how to trace sessions in the DB ? Also, is there any oracle configuration that can be set to handle this ?

[Updated on: Wed, 11 January 2012 04:29]

Report message to a moderator

Re: Select statement is blocking a delete statement (merged 2) [message #539078 is a reply to message #539077] Wed, 11 January 2012 04:33 Go to previous messageGo to next message
cookiemonster
Messages: 13975
Registered: September 2008
Location: Rainy Manchester
Senior Member
SQL TRACE
One obvious possibility that springs to mind is that the blocker is currently deleting the same row the blockee is trying to delete and is just being really slow.
This modifified version of the above query may help:
SELECT TO_CHAR(sysdate, 'DD-MON-YYYY HH24:MI:SS')
|| ' User '
||s1.username
|| '@'
|| s1.machine
|| ' ( SID= '
|| s1.sid
|| ' ) with the current statement: '
|| sql1cur.sql_text
||' and prev statement: '
|| sqlt1.sql_text
||' is blocking the SQL statement on '
|| s2.username
|| '@'
|| s2.machine
|| ' ( SID='
|| s2.sid
|| ' ) blocked SQL -> '
||sqlt2.sql_text AS blocking_status
FROM v$lock l1, v$session s1, v$lock l2 ,
v$session s2,v$sql sqlt1, v$sql sqlt2,
v$sql sql1cur
WHERE s1.sid =l1.sid
AND s2.sid =l2.sid 
AND sqlt2.sql_id= s2.sql_id 
AND sqlt1.sql_id= s1.prev_sql_id 
AND sql1cur.sql_id(+) = s1.sql_id
AND l1.BLOCK =1
AND l2.request > 0 
AND l1.id1 = l2.id1 
AND l2.id2 = l2.id2


EDIT: fixed wiki link

[Updated on: Wed, 11 January 2012 04:35]

Report message to a moderator

Re: Select statement is blocking a delete statement (merged 2) [message #539081 is a reply to message #539078] Wed, 11 January 2012 04:37 Go to previous messageGo to next message
cookiemonster
Messages: 13975
Registered: September 2008
Location: Rainy Manchester
Senior Member
Also you should read up on sql injection and start using bind variables. Your current code is bad idea from both a security and performance point of view.
Re: Select statement is blocking a delete statement (merged 2) [message #539097 is a reply to message #539081] Wed, 11 January 2012 05:13 Go to previous message
archana485
Messages: 22
Registered: June 2010
Junior Member
Ok, I will try this query next time the issue happens. Thanks for the tip on SQL Injection and Bind Variables.

Regards,
Archana
Previous Topic: ORA-01001: invalid cursor
Next Topic: ORA-01732
Goto Forum:
  


Current Time: Mon Jan 19 15:09:35 CST 2026