简体   繁体   中英

Vaadin + TabSheet + Grails Service = No session for current thread

Good evening. I am having a problem using Vaadin and the tabsheet component combined with a Grails service.

Here is what i am trying to do. I have a Vaadin UI that has a tabsheet as its main component, and two different tabs inside of it where i put some components: the first tab has a VerticalLayout with other nested components and the second tab has a formlayout as its root, and other visual components nested inside.

Now, what i want to do is whenever one switches from the first tab to the second one, data for the components in the second tab, which are persisted in a database, is loaded using a Grails Service and assigned to the components inside. To be specific, i just want an inlinedatefield and a slider value to be loaded with previously saved data whenever i switch to the second tab that contains said components. In order to implement this, i coded the addSelectedTabChangeListener and inside i called my service in order to populate the components of the second tab with the needed data.

But, whenever i switch to the second tab i get the following exception:

| Error 2015-11-14 00:43:36,072 [http-bio-8080-exec-8] ERROR server.DefaultErrorHandler  -
Message: org.springframework.dao.DataAccessResourceFailureException: Could not obtain current Hibernate Session; nested exception is org.hibernate.HibernateException: No Session found for current thread

I inject, inside my application UI, the service needed to get the data required by the components in the second tab, using the @AutoWired annotation. The exception above leads me to believe that switching to another tab, in fact, creates another thread and this causes problems for the service as, apparently, the hibernate session gets trashed.

Here is the problematic piece of code:

tabSheet.addSelectedTabChangeListener(new TabSheet.SelectedTabChangeListener() {
            @Override
            void selectedTabChange(TabSheet.SelectedTabChangeEvent selectedTabChangeEvent) {

                if(selectedTabChangeEvent.tabSheet.selectedTab.caption == "Programación de la Notificación") {
                    def savedNotificationProgramming = notificationProgrammingService.getNotificationProgramming()
                    GregorianCalendar savedTime = new GregorianCalendar()
                    savedTime.set(java.util.Calendar.HOUR_OF_DAY, savedNotificationProgramming?.hour)
                    savedTime.set(java.util.Calendar.MINUTE, savedNotificationProgramming?.minute)
                    println("hora: " + savedNotificationProgramming?.hour + "minuto: " + savedNotificationProgramming?.minute)
                    jobSettingsForm.numberOfDays.value = savedNotificationProgramming?.days as Double
                    jobSettingsForm.time.value = savedTime.time
                }

            }
        })

jobSettingsForm is a class that derives from FormLayout, and it is the one that becomes the root component of the second tab as its main container.

notificationProgrammingService is my grails service that i am injecting in MyUI app:

class MyUI extends UI {

    @Autowired
    NotificationProgrammingService notificationProgrammingService

Take note that i have another injected service that works flawlessly for a calendar component that i have on the first tab. So i know it's not got to do with incompatibilities between Grails services and Vaadin, or a problem with the @AutoWired annotation. It has to do with the changing from one tab to another and the hibernate session being discarded.

I greatly appreciate any help i could get with this problem. I just haven't been able to find enough information as to why this is happening. Thank you in advance

I solved this problem by enabling the opensessioninview option in Vaadin.config. Apparently, the hibernate session is being closed when I switch tabs, though I do not understand why it closes and this allows the session to be available when the tab switching is done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM