dcsimg
Thread Synchronization
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Kiran_Kumar
Posted On:   Friday, May 12, 2006 03:47 AM

Right now Im using Threads in my Programme.At a time i made minimum 6 threads to run.Each thread will do some operation finally it should fetch the collected data into DataBase. The Table has a primary key which is of type aut increment. So at a time 2 thread's can't fetch the data into Data Base.So i have used Synchronized Block in my code,where exactly the code related to fetching into DataBase.I have overcome that problem upto some extent.But after some time its throwing some exception as below......... -------------------------------------------------------------- org.hibernate.exception.ConstraintViolationException: could not insert: [Advance d.Blogs.SubUrlMaster] at org.hibernate.exception.ErrorCodeConverter.convert(ErrorC   More>>

Right now Im using Threads in my Programme.At a time i made minimum 6 threads to run.Each thread will do some operation finally it should fetch the collected data into DataBase.




The Table has a primary key which is of type aut increment. So at a time 2 thread's can't fetch the data into Data Base.So i have used Synchronized Block in my code,where exactly the code related to fetching into DataBase.I have overcome that problem upto some extent.But after some time its throwing some exception as below.........





-------------------------------------------------------------- org.hibernate.exception.ConstraintViolationException: could not insert: [Advance d.Blogs.SubUrlMaster] at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter .java:74) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelp er.java:43) at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntit yPersister.java:1839) at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntit yPersister.java:2171) at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.ja va:46) at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:136) at org.hibernate.event.def.AbstractFlushingEventListener.performExecutio ns(AbstractFlushingEventListener.java:274) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlus hEventListener.java:27) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:669) at Advanced.Blogs.CrawlSubUrls.FetchToDb(CrawlSubUrls.java:224) at Advanced.Blogs.CrawlSubUrls.putSubUrl(CrawlSubUrls.java:171) at Advanced.Blogs.CrawlSubUrls.process_CCount0(CrawlSubUrls.java:140) at Advanced.Blogs.CrawlSubUrls.CrawlLevelTest(CrawlSubUrls.java:85) at Advanced.Blogs.CrawlSubUrls.run(CrawlSubUrls.java:66) Caused by: java.sql.SQLException: Duplicate entry '6585' for key 1 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2851) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1531) at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedSt atement.java:1366) at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPrepared Statement.java:952) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java :1974) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java :1897) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java :1758) at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.j ava:22) at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntit yPersister.java:1823) ... 13 more ----------------------------------------------------------------




So Can u plz suggest me the best approach...... And here is the code .......




synchronized(this){

Transaction tx = null;

tx = session1.beginTransaction();

SubUrlMaster surm = new SubUrlMaster();

surm.setUrlId(urlid_db); surm.setSubUrl(str_CURL);
surm.setStatus(0); surm.setCrawledOntology("0");
surm.setCrawlDepth(crawlLevel_db);
surm.setCrawlTimeStamp(t_stamp);

synchronized(this){

session1.saveOrUpdate(surm);

tx.commit(); }

System.out.println("SUB URL MASTER UPDATED");
session1.flush() ;

}


Thanks & Regards

Kiran Kumar.K    <<Less

Re: Thread Synchronization

Posted By:   Kiran_Kumar  
Posted On:   Wednesday, May 17, 2006 07:04 AM

Hi All,

This Issue was posted by me.I got a Permanent Solution for this.The above piece of code resulted Dead Lock.

Before we use synchronized blocks or statements,make sure that you are not supposed to call a synchronized block/stmt from another synchronized block/stmt.It will results DeadLock.

So i have approached a techniqued named "Mutex"

Use in the following manner



public static int mutext = 0;

//Ur function starts here

Inside Ur Functon.......

synchronized(this){

while(mutex!=0){

Thread.sleep(100);

};

mutex++;

}

//Here the code which is going to DataBase or writing to anything



mutex--;


This will definetly solves Thread Synchronization Problmes.



Regards

Kiran Kumar.

Re: Thread Synchronization

Posted By:   Edward_Harned  
Posted On:   Friday, May 12, 2006 11:46 AM

You're using a synchronized (this) inside a previous synchronided (this).


Try taking out one of the sync statements.

About | Sitemap | Contact