8 Replies Latest reply on Aug 13, 2008 10:28 AM by Andrea

    Multiple Databases

    Kulvinder Singh Newbie

      I have a requirement whereby my application instance will be accessing multiple similar (in scheme) databases based on the user. How should I go about it.


      I have considered the following, please suggest:


      Define the managed persistence units and datasources in components.xml and xxx-ds.xml in application scope with distinct name.


      Authenticate the user and pull the respective EntityManager from the application context into Session context with name entityManager which is usually accessed by application code. All component searches will end in session context and the correct EntityManager will be returned.


      I am a newbie with seam so please suggest the appropriate.

        • 1. Re: Multiple Databases
          Tom Goring Apprentice

          If your requirement is to have users data set split it may be a better idea to use a hibernate filter (e.g. Locale, Company, etc) and add that to each table that needs to be seperate.


          Then you just have 1 DB split logically for sets of users.... which may be more manageable.  All this would be transparent to you application code.


             

          • 2. Re: Multiple Databases
            Kulvinder Singh Newbie

            Thanks for reply.



            No that's not about users only. We are working on SaaS model and have multi-tenant architecture whereby for each client we will have a different database instance with same schema. We will use clustered jboss servers.


            Once establishing that the user is belonging to which database through centerlised DB or properties file, subsequent requests by that user will require corresponding PersistenceContext.


            Another question Tom, how could I involve others on this?

            • 3. Re: Multiple Databases
              Gavin King Master

              Try writing a custom Hibernate ConnectionProvider.

              • 4. Re: Multiple Databases
                Kulvinder Singh Newbie

                Can you provide existing example for Custom ConnectionProvider?



                Also I tried using an interceptor (PostConstruct) for components extending EntityQuery and set the entityManager. it works but seems to be binding in terms of extending EntityQuery class.


                Following is the code.



                MyIdentity identity = (MyIdentity) Identity.instance()
                
                EntityManager entityManager = (EntityManager) Component.getInstance(identity.getPersistenceContext());
                
                          
                
                try {
                
                     EntityQuery eq=(EntityQuery) ctx.getTarget();
                
                     eq.setEntityManager(entityManager);
                
                }catch(ClassCastException cce) {}
                
                


                • 6. Re: Multiple Databases
                  Kulvinder Singh Newbie

                  could you please provide a link to already implemented connectionprovider across databases.


                  how can we get the users request or session in custom connection provider in order to get the corresponding connection from the pool. As I understand that connection provide will have to be maintaining the connection pools for each database. Please suggest.

                  • 7. Re: Multiple Databases
                    Nilon de O. Barroso Filho Newbie

                    Example of ConnectionProvider with multiple databases and Seam.


                    The property ctx makes it possible to capture an object of type javax.sql.DataSource. When a user is  logged and before any use of EntityManager, it created a context of variable called 'dataSourceName', and it is associated with the name of the DataSource that the user selected on the login screen.



                    Example: Contexts.getSessionContext().set("dataSourceName", userBean.getDataSourceName());




                    In the method getConnection(), the class ' org.jboss.seam.contexts.Contexts' provides an instance of the class' Context '. This class has information of the user's session which requested the DataSource. It is possible to capture the DataSource name of the user and then get an instance of the class 'javax.sql.DataSource'.


                    <property name="hibernate.connection.provider_class" value="br.com.aspec.utils.AspConnectionProvider" />





                    Following is the code.



                    import java.sql.Connection;
                    import java.sql.SQLException;
                    import java.util.Properties;
                    
                    import javax.naming.InitialContext;
                    import javax.naming.NamingException;
                    
                    import org.hibernate.HibernateException;
                    import org.hibernate.connection.ConnectionProvider;
                    import org.jboss.seam.contexts.Contexts;
                    
                    public class AspConnectionProvider implements ConnectionProvider
                    {
                    
                        private InitialContext ctx;
                        
                        private String dataSourceNameDefault = "ddcDatasource";
                        
                        
                        public void configure(Properties props) throws HibernateException
                        {
                            try
                            {
                                ctx = new InitialContext(props);
                            } catch (NamingException e)
                            {
                                throw new HibernateException(e.getMessage());
                            }
                        }
                    
                        public Connection getConnection() throws SQLException
                        {
                            
                            try
                            {
                                if (Contexts.getSessionContext() != null)
                                {
                                    String dataSourceName = (String)Contexts.getSessionContext().get("dataSourceName");
                                    javax.sql.DataSource ds = (javax.sql.DataSource)ctx.lookup("java:/" + dataSourceName);
                                    return ds.getConnection();
                                }
                                else
                                {
                                    javax.sql.DataSource ds = (javax.sql.DataSource)ctx.lookup("java:/" + dataSourceNameDefault);
                                    return ds.getConnection();
                                }
                            } catch (NamingException e)
                            {
                                throw new SQLException(e.getMessage());
                            }
                            
                            
                        }
                    
                        public void closeConnection(Connection conn) throws SQLException
                        {
                            if (conn != null)
                            {
                                conn.close();
                            }
                        }
                    
                        public void close()
                        {
                        }
                    
                        @Override
                        public boolean supportsAggressiveRelease()
                        {
                            return false;
                        }
                    
                    
                    }
                    


                    In persistence.xml file


                    <property name="hibernate.connection.provider_class" value="br.com.aspec.utils.AspConnectionProvider" />



                    Note: This solution has not been sufficiently tested. Any problems found, please communicate.

                    • 8. Re: Multiple Databases
                      Andrea Newbie

                      Hi,


                      I tried to implement your solution, but with no success.


                      My environment:



                      • Framework: Seam 1.2.GA

                      • JBoss: 4.0.5.GA

                      • VM: jdk 1.5.0-09-b03

                      • DB: postgres

                      • O/S: Linux Debian



                      The scenario:


                      I have the same db structure, but different data and I'd like to switch from one db to the other before a user can login (login data is different and is stored in each db).
                      The webclient starts from a simple page where the user can select the datasource. The datasourceName is set and the login page is shown. I input the login data, I login and then I get an exception saying that the session is closed. I think this occurs the first time I try to inject my EntityManager into the first bean called upon login, but I don't understant what's wrong.
                      I attach my code and the exception. What am I doing wrong?


                      My code:


                      persistence.xml


                      <persistence>
                           <persistence-unit name="myPersistenceUnit" transaction-type="JTA">
                                <provider>org.hibernate.ejb.HibernatePersistence</provider>
                                <jta-data-source>java:/MyDS</jta-data-source>
                                <properties>
                                     <property name="hibernate.connection.provider_class" value="com.spotme.utils.db.MyConnectionProvider"></property>
                                     <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
                                     <property name="hibernate.jdbc.batch_size">50</property>               
                                     <property name="transaction.flush_before_completion">true</property>
                                     <property name="jboss.entity.manager.factory.jndi.name">java:/EntityManagerFactories/myEMF</property>
                                     <property name="hibernate.transaction.auto_close_session">true</property>
                                     <property name="hibernate.transaction.flush_before_completion">true</property>
                                </properties>
                           </persistence-unit>
                      </persistence>



                      Before this I tried not to specify <jta-data-source>java:/MyDS</jta-data-source>, but JBoss couldn't start without the datasource. Specifying it doesn't convince me but I thought it will be handled as default datasource:


                      10:31:40,395 WARN  [ServiceController] Problem starting service persistence.units:ear=my.ear,jar=my.jar,unitName=myPersistenceUnit
                      java.lang.RuntimeException: You have not defined a jta-data-source for a JTA enabled persistence context named: myPersistenceUnit



                      hibernate-ds.xml


                      <datasources>
                          <local-tx-datasource>
                              <jndi-name>MyDS</jndi-name>
                              <connection-url>jdbc:postgresql://localhost:5432/db1</connection-url>
                                <user-name>uname</user-name>
                              <password></password>
                              <driver-class>org.postgresql.Driver</driver-class>
                              <metadata>
                               <type-mapping>PostgreSQL 8.0</type-mapping>
                            </metadata>
                          </local-tx-datasource>
                           
                          <local-tx-datasource>
                              <jndi-name>MyDS2</jndi-name>
                              <connection-url>jdbc:postgresql://localhost:5432/db2</connection-url>
                                <user-name>uname</user-name>
                              <password></password>
                              <driver-class>org.postgresql.Driver</driver-class>
                              <metadata>
                               <type-mapping>PostgreSQL 8.0</type-mapping>
                            </metadata>
                          </local-tx-datasource>
                      </datasources>



                      components.xml


                      <component name="myEM" class="org.jboss.seam.core.ManagedPersistenceContext">
                           <property name="persistenceUnitJndiName">
                                java:/EntityManagerFactories/myEMF
                           </property>
                      </component>



                      DataSourceSetterActionBean


                      import javax.ejb.Stateless;
                      import org.apache.log4j.Logger;
                      import org.jboss.seam.annotations.Name;
                      import org.jboss.seam.contexts.Contexts;
                      
                      @Stateless
                      @Name("DSSetter")
                      public class DataSourceSetterActionBean implements DataSourceSetterAction {
                      
                          private static final Logger log = Logger.getLogger(DataSourceSetterActionBean.class);
                          private String datasourceName;
                          
                          public String gotoLogin() {
                              Contexts.getSessionContext().set("dataSourceName", datasourceName);
                              return "login";
                          }
                          public String getDatasourceName() {
                              return datasourceName;
                          }
                          public void setDatasourceName(String datasourceName) {
                              this.datasourceName = datasourceName;
                          }
                      }



                      setDS.xhtml


                      <h:form>
                      <table><tr>
                           <td>
                           <h:selectOneMenu value="#{DSSetter.datasourceName}">
                                <f:selectItem itemValue="MyDS" itemLabel="DataSource 1" />
                                <f:selectItem itemValue="MyDS2" itemLabel="DataSource 2" />
                           </h:selectOneMenu>
                           </td>
                           <td>
                                <t:commandButton action="#{DSSetter.gotoLogin}" value=" Set "/>
                           </td>
                      </tr></table>
                      </h:form>



                      LoginActionBean (called after datasource selection)


                      @Stateless
                      @Name("login")
                      public class LoginAction implements Login {
                      
                          @Logger
                          private Log log;
                           
                          @In(required=false) @Out(required=false)
                          private User user;
                      
                          public String login() {
                              log.info("Checking login info for user: " + user.getName());
                              
                              User userReference = authenticate(user.getName(), user.getPassword());
                              if(userReference != null) {
                                  log.info(userReference + " logged in! Role: "+userReference.getRole().name());
                                  
                                  Identity id = Identity.instance();
                                  id.setUsername(userReference.getName());
                                  id.setPassword(userReference.getPassword());
                      
                                  try {
                                      id.authenticate();
                                  } catch (LoginException e) {
                                      e.printStackTrace();
                                  }
                                  id.addRole(userReference.getRole().name());
                      
                                  Context sessionContext = Contexts.getSessionContext();
                                  sessionContext.set("loggedIn", true);
                                  
                                  this.user = userReference;
                                  log.info("Setting current " + user);
                                  
                                  return "home";
                              }
                              else {
                                  log.info("Invalid Login!");
                                  FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Invalid login"));
                                  return null;
                              }
                          }
                      
                          public boolean authenticate() { 
                              log.info("Calling public authenticate");
                              
                              Identity id = Identity.instance();
                              User userReference = authenticate(id.getUsername(), id.getPassword());
                              if(userReference == null) {
                                  return false;   
                              }
                              else {
                                  return true;
                              }
                          }
                          
                          private User authenticate(String username, String password) {
                              log.info("Calling private authenticate");
                      
                              if(username == null)
                                  return null;
                      
                              log.info("Authenticating user " + username);
                              try {
                                    EntityManager em = (EntityManager)Component.getInstance("myEM", true);
                                  
                                     User user = (User)em
                                  .createQuery("from User where name=:name and password=:password")
                                  .setParameter("name", username)
                                  .setParameter("password", password)
                                  .getSingleResult();
                                  
                                  return user; 
                              }
                              catch (NoResultException e) {
                                  return null;
                              }
                              catch (NonUniqueResultException e) {
                                  return null;
                              }
                          }
                      }



                      Finally, the exception that I get:


                      10:24:03,705 INFO  [DataSourceSetterActionBean] Setting datasource name: MyDS
                      10:24:04,037 INFO  [LoginAction] Calling public authenticate
                      10:24:04,040 INFO  [LoginAction] Calling private authenticate
                      10:24:13,258 INFO  [LoginAction] Checking login info for user: admin
                      10:24:13,259 INFO  [LoginAction] Calling private authenticate
                      10:24:13,259 INFO  [LoginAction] Authenticating user admin
                      10:24:14,313 INFO  [MyConnectionProvider] Getting connection
                      10:24:14,345 INFO  [MyProvider] Closing connection
                      10:24:14,363 INFO  [LoginAction] User (name: admin, role: ADMIN, bid:ID_ALL) logged in! Role: ADMIN
                      10:24:14,380 INFO  [LoginAction] Calling public authenticate
                      10:24:14,383 INFO  [LoginAction] Calling private authenticate
                      10:24:14,383 INFO  [LoginAction] Authenticating user admin
                      10:24:14,384 INFO  [LoginAction] Got em org.jboss.seam.persistence.EntityManagerProxy@1bf5ee7
                      10:24:14,420 INFO  [MyConnectionProvider] Getting connection
                      10:24:14,426 INFO  [MyConnectionProvider] Closing connection
                      10:24:14,444 INFO  [LoginAction] Setting current User (name: admin, role: ADMIN, bid:ID_ALL)
                      10:24:14,587 ERROR [SeamPhaseListener] uncaught exception
                      org.hibernate.SessionException: Session is closed!
                              at org.hibernate.impl.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:49)
                              at org.hibernate.impl.SessionImpl.setFlushMode(SessionImpl.java:1286)
                              at org.jboss.seam.persistence.HibernatePersistenceProvider.setFlushModeManual(HibernatePersistenceProvider.java:39)
                              at org.jboss.seam.core.ManagedPersistenceContext.setEntityManagerFlushMode(ManagedPersistenceContext.java:237)
                              at org.jboss.seam.core.ManagedPersistenceContext.changeFlushMode(ManagedPersistenceContext.java:222)
                              at org.jboss.seam.core.PersistenceContexts.changeFlushModes(PersistenceContexts.java:73)
                              at org.jboss.seam.core.PersistenceContexts.beforeRender(PersistenceContexts.java:82)
                              at org.jboss.seam.jsf.AbstractSeamPhaseListener.beforeRender(AbstractSeamPhaseListener.java:238)
                              at org.jboss.seam.jsf.SeamPhaseListener.beforePhase(SeamPhaseListener.java:56)
                              at org.apache.myfaces.lifecycle.PhaseListenerManager.informPhaseListenersBefore(PhaseListenerManager.java:70)
                              at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:373)
                              at javax.faces.webapp.FacesServlet.service(FacesServlet.java:138)
                              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
                              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                              at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:144)
                              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                              at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:79)
                              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                              at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:57)
                              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                              at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
                              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                              at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
                              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                              at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
                              at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
                              at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175)
                              at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)
                              at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
                              at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
                              at org.jboss.web.tomcat.tc5.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:156)
                              at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
                              at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
                              at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
                              at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
                              at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
                              at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
                              at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
                              at java.lang.Thread.run(Thread.java:595)
                      10:24:14,672 WARN  [Interpolator] exception interpolating string: #{org.jboss.seam.handledException.message}
                      javax.faces.el.PropertyNotFoundException: Bean: org.hibernate.SessionException, property: message
                              at org.apache.myfaces.el.PropertyResolverImpl.getProperty(PropertyResolverImpl.java:423)
                              at org.apache.myfaces.el.PropertyResolverImpl.getValue(PropertyResolverImpl.java:82)
                              at org.apache.myfaces.el.ELParserHelper$MyPropertySuffix.evaluate(ELParserHelper.java:532)
                              at org.apache.commons.el.ComplexValue.evaluate(ComplexValue.java:145)
                              at org.apache.myfaces.el.ValueBindingImpl.getValue(ValueBindingImpl.java:383)
                              at org.jboss.seam.core.Expressions$1.getValue(Expressions.java:69)
                              at org.jboss.seam.core.Interpolator.interpolateExpressions(Interpolator.java:88)
                              at org.jboss.seam.core.Interpolator.interpolate(Interpolator.java:67)
                              at org.jboss.seam.core.FacesMessages.createFacesMessage(FacesMessages.java:348)
                              at org.jboss.seam.core.FacesMessages.createFacesMessage(FacesMessages.java:356)
                              at org.jboss.seam.core.FacesMessages$1.run(FacesMessages.java:402)
                              at org.jboss.seam.core.FacesMessages.runTasks(FacesMessages.java:133)
                              at org.jboss.seam.core.FacesMessages.afterPhase(FacesMessages.java:143)
                              at org.jboss.seam.jsf.SeamPhaseListener.afterPhase(SeamPhaseListener.java:108)
                              at org.apache.myfaces.lifecycle.PhaseListenerManager.informPhaseListenersAfter(PhaseListenerManager.java:89)
                              at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:391)
                              at javax.faces.webapp.FacesServlet.service(FacesServlet.java:138)
                              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
                              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                              at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:144)
                              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                              at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:79)
                              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                              at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:57)
                              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                              at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
                              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                              at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
                              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                              at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
                              at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
                              at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175)
                              at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)
                              at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
                              at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
                              at org.jboss.web.tomcat.tc5.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:156)
                              at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
                              at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
                              at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
                              at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
                              at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
                              at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
                              at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
                              at java.lang.Thread.run(Thread.java:595)
                      10:24:14,685 ERROR [SeamPhaseListener] uncaught exception
                      org.hibernate.SessionException: Session is closed!
                              at org.hibernate.impl.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:49)
                              at org.hibernate.impl.SessionImpl.setFlushMode(SessionImpl.java:1286)
                              at org.hibernate.ejb.AbstractEntityManagerImpl.setFlushMode(AbstractEntityManagerImpl.java:324)
                              at org.jboss.seam.persistence.EntityManagerProxy.setFlushMode(EntityManagerProxy.java:143)
                              at org.jboss.seam.core.ManagedPersistenceContext.setEntityManagerFlushMode(ManagedPersistenceContext.java:231)
                              at org.jboss.seam.core.ManagedPersistenceContext.changeFlushMode(ManagedPersistenceContext.java:222)
                              at org.jboss.seam.core.PersistenceContexts.changeFlushModes(PersistenceContexts.java:73)
                              at org.jboss.seam.core.PersistenceContexts.afterRender(PersistenceContexts.java:88)
                              at org.jboss.seam.jsf.AbstractSeamPhaseListener.afterRender(AbstractSeamPhaseListener.java:251)
                              at org.jboss.seam.jsf.SeamPhaseListener.afterPhase(SeamPhaseListener.java:115)
                              at org.apache.myfaces.lifecycle.PhaseListenerManager.informPhaseListenersAfter(PhaseListenerManager.java:89)
                              at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:391)
                              at javax.faces.webapp.FacesServlet.service(FacesServlet.java:138)
                              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
                              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                              at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:144)
                              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                              at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:79)
                              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                              at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:57)
                              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                              at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
                              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                              at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
                              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                              at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
                              at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
                              at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175)
                              at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)
                              at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
                              at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
                              at org.jboss.web.tomcat.tc5.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:156)
                              at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
                              at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
                              at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
                              at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
                              at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
                              at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
                              at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
                              at java.lang.Thread.run(Thread.java:595)
                      10:24:14,698 ERROR [SeamPhaseListener] swallowing exception
                      org.hibernate.SessionException: Session is closed!
                              at org.hibernate.impl.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:49)
                              at org.hibernate.impl.SessionImpl.setFlushMode(SessionImpl.java:1286)
                              at org.hibernate.ejb.AbstractEntityManagerImpl.setFlushMode(AbstractEntityManagerImpl.java:324)
                              at org.jboss.seam.persistence.EntityManagerProxy.setFlushMode(EntityManagerProxy.java:143)
                              at org.jboss.seam.core.ManagedPersistenceContext.setEntityManagerFlushMode(ManagedPersistenceContext.java:231)
                              at org.jboss.seam.core.ManagedPersistenceContext.changeFlushMode(ManagedPersistenceContext.java:222)
                              at org.jboss.seam.core.PersistenceContexts.changeFlushModes(PersistenceContexts.java:73)
                              at org.jboss.seam.core.PersistenceContexts.afterRender(PersistenceContexts.java:88)
                              at org.jboss.seam.jsf.AbstractSeamPhaseListener.afterRender(AbstractSeamPhaseListener.java:251)
                              at org.jboss.seam.jsf.SeamPhaseListener.afterPhase(SeamPhaseListener.java:115)
                              at org.apache.myfaces.lifecycle.PhaseListenerManager.informPhaseListenersAfter(PhaseListenerManager.java:89)
                              at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:391)
                              at javax.faces.webapp.FacesServlet.service(FacesServlet.java:138)
                              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
                              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                              at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:144)
                              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                              at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:79)
                              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                              at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:57)
                              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                              at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
                              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                              at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
                              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                              at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
                              at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
                              at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175)
                              at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)
                              at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
                              at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
                              at org.jboss.web.tomcat.tc5.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:156)
                              at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
                              at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
                              at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
                              at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
                              at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
                              at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
                              at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
                              at java.lang.Thread.run(Thread.java:595)



                      Sorry for the big bunch of code, but I guess it's all necessary to understand the exception. Thanx!


                      Andrea