0

I have a Grails service containing a method

@Transactional def method(param1, param2, param3, param4) { SomeClass obj = new SomeClass(param1: param1, param2: param2, param3: param3, param4: param4) obj.save(flush:true, failOnError:true) } 

The method fails silently, even though I provided the "failOnError" parameter. This already took me some time to figure out, so I changed it to:

@Transactional def method(param1, param2, param3, param4) { try { SomeClass obj = new SomeClass(param1: param1, param2: param2, param3: param3, param4: param4) obj.save(flush:true, failOnError:true) } catch (Throwable t) { t.printStackTrace() } } 

Now, finally, I get to see the error message which is: "No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here". This is curious. I always believed "@Transactional" would provide me with a valid Hibernate session. So I am a little at my wits' end here. Does anyone have any idea what the problem might be?

1 Answer 1

1

use session.open() before accessing a database and close by specifying explicitly as one solution. And also a good answer detail is Here by Burt Beckwith.

Please use his example if it helps .

void deviceDisconnected(String mobileId, String wifiIp){ try { UserMobile.withTransaction { tx -> def mobile = Mobile.findByMobileId(mobileId) def userMobile = UserMobile.findByMobileAndWifiIp(mobile, wifiIp) userMobile.action = Constants.MOBILE_STATUS_DISCONNECTED userMobile.alarmStatus = Constants.ALARM_STATUS_TURNED_ON userMobile.modifiedDate = new Date() userMobile.save(flush: true) } } catch(e) { e.printStackTrace() } } 
Sign up to request clarification or add additional context in comments.

1 Comment

That solves the problem. Thank you. But I wanted to really understand what is going on and did some further research. Turns out "@Transactional" is not equal "@Transactional". There is a Spring version and a Grails version (grails.transaction.Transactional) of the annotation and I used the Spring version instead of the Grails variant, which was the initial cause to my problem. Thanks for the help anyways.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.