0

I have Class in src/groovy . I want to use my service here . but error occurred "No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here ". i try to debug but not able to find . can you please help me that what is my mistake .

class ListenerSession implements HttpSessionListener { def transactionService = new TransactionService () public ListenerSession() { } public void sessionCreated(HttpSessionEvent sessionEvent){ } public void sessionDestroyed(HttpSessionEvent sessionEvent) { HttpSession session = sessionEvent.getSession(); User user=session["user"] if(user){ try{ java.util.Date date = session['loginDate'] transactionService.updateUserLastLogin(user,date) -----}catch (Exception e) { println e } 

code in service is:

def updateUserLastLogin(User user,Date date){ try{ User.withTransaction{ println "121212" user.lastLogin=date user.loginDuration=new Date().time - user?.lastLogin?.time def x=user.save() } }catch (Exception e) { println e } } 
1
  • you should consider to write your code into a "code block", so that it's better to read... did you add your listener to the web.xml file? also, this should give you some advice: stackoverflow.com/questions/4088259/… Commented Dec 6, 2012 at 6:54

2 Answers 2

2

Don't instantiate services with new. If they use nearly any piece of Grails framework, that piece won't work - like GORM session in this case.

Here's an exactly your question: http://grails.1312388.n4.nabble.com/Injecting-Grails-service-into-HttpSessionListener-can-it-be-done-td1379074.html

with Burt's answer:

ApplicationContext ctx = (ApplicationContext)ServletContextHolder. getServletContext().getAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT) transactionService = (TransactionService) ctx.getBean("transactionService") 
Sign up to request clarification or add additional context in comments.

Comments

1

Grails won't inject your service for you in the src/groovy level and just declaring a new instance of TransactionService will not give you all the goodies (hence your error). You need to get your instance form the spring context like so...

 import org.codehaus.groovy.grails.web.context.ServletContextHolder as SCH import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes as GA class ListenerSession implements HttpSessionListener { public ListenerSession() { } public void sessionCreated(HttpSessionEvent sessionEvent){ } public void sessionDestroyed(HttpSessionEvent sessionEvent) { HttpSession session = sessionEvent.getSession(); User user=session["user"] if(user){ try{ java.util.Date date = session['loginDate'] def ctx = SCH.servletContext.getAttribute(GA.APPLICATION_CONTEXT) def transactionService = ctx.transactionService transactionService.updateUserLastLogin(user,date) }catch (Exception e) { println e } } } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.