-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Newbie question: How to set timeout for persisting object
PostPosted: Wed Mar 24, 2010 11:42 am 
Newbie

Joined: Wed Mar 24, 2010 11:24 am
Posts: 2
Hello experts,

how can I set a time-out for persisting an object ?

In my example object "reqInvoice" shall be persisted into database. If table is locked in DB insert/update will wait until table is unlocked. Unfortunately I don't want to wait years. How can I set a time-out ?

Code:
SessionFactoryManager.init(JNDI_URL);                     
SessionFactory sf = SessionFactoryManager.getInstance(JNDI_URL);
   
Session session = sf.openSession(); 
session.clear();
session.setFlushMode(FlushMode.COMMIT);

Transaction transaction = transaction = session.getTransaction();
      
transaction = session.beginTransaction();
...
Invoice reqInvoice = ...;
...
session.save(reqInvoice);
transaction.commit();
...
session.close();



Br,

Dennis


Top
 Profile  
 
 Post subject: Re: Newbie question: How to set timeout for persisting object
PostPosted: Wed Mar 24, 2010 3:07 pm 
Newbie

Joined: Wed Mar 24, 2010 3:03 pm
Posts: 8
Simply use: yourObject.setQueryTimeout(300);

---------
Info:

void java.sql.Statement.setQueryTimeout(int seconds) throws SQLException

setQueryTimeout
public void setQueryTimeout(int seconds)
throws SQLException
Sets the number of seconds the driver will wait for a Statement object to execute to the given number of seconds. If the limit is exceeded, an SQLException is thrown.

Parameters:
seconds - the new query timeout limit in seconds; zero means there is no limit
Throws:
SQLException - if a database access error occurs or the condition seconds >= 0 is not satisfied
See Also:
getQueryTimeout()


Top
 Profile  
 
 Post subject: Re: Newbie question: How to set timeout for persisting object
PostPosted: Wed Mar 24, 2010 3:07 pm 
Newbie

Joined: Wed Mar 24, 2010 3:03 pm
Posts: 8
Simply use: yourObject.setQueryTimeout(300);

---------
Info:

void java.sql.Statement.setQueryTimeout(int seconds) throws SQLException

setQueryTimeout
public void setQueryTimeout(int seconds)
throws SQLException
Sets the number of seconds the driver will wait for a Statement object to execute to the given number of seconds. If the limit is exceeded, an SQLException is thrown.

Parameters:
seconds - the new query timeout limit in seconds; zero means there is no limit
Throws:
SQLException - if a database access error occurs or the condition seconds >= 0 is not satisfied
See Also:
getQueryTimeout()


Top
 Profile  
 
 Post subject: Re: Newbie question: How to set timeout for persisting object
PostPosted: Thu Mar 25, 2010 8:46 am 
Newbie

Joined: Wed Mar 24, 2010 11:24 am
Posts: 2
Hi,

for using void java.sql.Statement.setQueryTimeout(int seconds) throws SQLException I would have need to
create a statement. Actually I don't want to do that, but work with my persisting java object using hibernate.

Any idea about that ?

Br,

Dennis


Top
 Profile  
 
 Post subject: Re: Newbie question: How to set timeout for persisting object
PostPosted: Fri Mar 26, 2010 10:15 am 
Newbie

Joined: Wed Mar 24, 2010 3:03 pm
Posts: 8
Alright, you can try setTimeOut() method, it's all in Hibernate documentation. You need to dip or search a little more. Below is the sample code, hope it help.

Session sess = factory.openSession();
try {
//set transaction timeout to 3 seconds
sess.getTransaction().setTimeout(3);
sess.getTransaction().begin();

// do some work
...

sess.getTransaction().commit()
}
catch (RuntimeException e) {
sess.getTransaction().rollback();
throw e; // or display error message
}
finally {
sess.close();
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.