Announcement Announcement Module
Collapse
No announcement yet.
Transaction doesnt begin Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Transaction doesnt begin

    Hi, first sorry in advance for my english.
    Hereīs the problem. Iīm trying to implement this tutorial (http://facestutorials.icefaces.org/t...ity-basic.html) from IceFaces using Spring Security and Hibernate.
    I get this error: org.hibernate.HibernateException: createCriteria is not valid without active transaction.
    I donīt understand why but the transaction doesnt begin!! When I debug the UsuarioDAO have the sessionFactory, a session open, and a transaction but is not active. I have the transactionManager defined, advice and everything like the spring documentation in chapter 9 explains. This used to work perfectlly but with a RMI service. Now with a web app is not working.

    What Iīm missing?
    This are my files:

    WEB.XML
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/applicationContext.xml</param-value>
        </context-param>
    
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
        <filter>
            <filter-name>springSecurityFilterChain</filter-name>
            <filter-class> org.springframework.web.filter.DelegatingFilterProxy         </filter-class>
        </filter>
    
        <filter-mapping>
            <filter-name>springSecurityFilterChain</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
       ......
       somthing else
       ......
    </web-app>
    ApplicationContext.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:p="http://www.springframework.org/schema/p"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:security="http://www.springframework.org/schema/security"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.4.xsd">
    
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://localhost/tridopoll"/>
            <property name="username" value="root"/>
            <property name="password" value="xxxxxx"/>
        </bean>
        
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
            <property name="dataSource"><ref local="dataSource" /></property>
            <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"/>
            <property name="configLocation" value="/WEB-INF/hibernate.cfg.xml"/>
        </bean>
        
        <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory"><ref local="sessionFactory"/></property>
        </bean>
    
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="get*" read-only="true"/>
                <tx:method name="*"/>
            </tx:attributes>
        </tx:advice>
    
        <aop:config>
            <aop:pointcut id="service" expression="execution(* servicios..*.*(..))"/>
            <aop:advisor advice-ref="txAdvice" pointcut-ref="service"/>
        </aop:config>
    
    <!-- ************************* Security ********************************-->
       <security:http auto-config="true" access-denied-page="/accesoDenegado.iface">
            <security:intercept-url pattern="/secured/**"
                                    access="ROLE_ALLACCESS, ROLE_URLACCESS"/>
            <security:form-login login-page="/login.iface"
                                 default-target-url="/secured/sistema.iface"/>
            <security:logout logout-success-url="/login.iface"/>
        </security:http>
    
        <security:authentication-provider user-service-ref="userDetailsService"/>
    
        <bean name="usuarioDao" class="dao.impl.UsuarioDAOHibernateImpl">
           <property name="sessionFactory"><ref bean="sessionFactory"/></property>
        </bean>    
    
    <!--************************* Servicios ******************************** -->
    
        <bean id="userDetailsService"
              class="servicios.impl.UserDetailsServiceImpl">
            <constructor-arg ref="usuarioDao"/>
        </bean>
    
    </beans>
    hibernate.cfg.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
      <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://001tec10:3306/tridopoll</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">swi966</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!-- Use the C3P0 connection pool.-->
        <property name="hibernate.c3p0.max_size">5</property>
        <property name="hibernate.c3p0.min_size">0</property>
        <property name="hibernate.c3p0.timeout">5000</property>
        <property name="hibernate.c3p0.max_statements">100</property>
        <property name="hibernate.c3p0.idle_test_period">300</property>
        <property name="hibernate.c3p0.acquire_increment">2</property>
        <property name="current_session_context_class">thread</property>
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
        <property name="cache.use_query_cache">false</property>
        <property name="cache.use_minimal_puts">false</property>
        <property name="max_fetch_depth">3</property>
        <mapping class="modelo.Usuario"/>
      </session-factory>
    </hibernate-configuration>
    UserDetailsServiceImpl.java
    Code:
    package servicios.impl;
    
    import modelo.Usuario;
    import dao.UsuarioDAO;
    import org.springframework.dao.DataAccessException;
    import org.springframework.security.GrantedAuthority;
    import org.springframework.security.GrantedAuthorityImpl;
    import org.springframework.security.userdetails.UserDetails;
    import org.springframework.security.userdetails.UserDetailsService;
    import org.springframework.security.userdetails.UsernameNotFoundException;
    
    public class UserDetailsServiceImpl implements UserDetailsService {
        private UsuarioDAO usuarioDao;
    
        public UserDetailsServiceImpl(UsuarioDAO userDao) {
            this.usuarioDao = userDao;
        }
    
        public UserDetails loadUserByUsername(String login)
                throws UsernameNotFoundException, DataAccessException {
            Usuario user = usuarioDao.findUser(login);
            if (user == null)
                throw new UsernameNotFoundException("Usuario no encontrado: " + login);
            else {
                return makeUser(user);
            }
        }
    
        private org.springframework.security.userdetails.User makeUser(Usuario user) {
            return new org.springframework.security.userdetails.User(user.getLogin(), user
                    .getPassword(), true, true, true, true,
                    makeGrantedAuthorities(user));
        }
    
        private GrantedAuthority[] makeGrantedAuthorities(Usuario user) {
            GrantedAuthority[] result = new GrantedAuthority[user.getRoles().size()];
            int i = 0;
            for (String role : user.getRoles()) {
                result[i++] = new GrantedAuthorityImpl(role);
            }
            return result;
        }
    
    }
    UsuarioDAOHibernateImpl.java
    Code:
    package dao.impl;
    
    import dao.UsuarioDAO;
    import modelo.Usuario;
    import org.hibernate.Criteria;
    import org.hibernate.criterion.Restrictions;
    
    public class UsuarioDAOHibernateImpl extends GenericDAOHibernateImpl<Usuario,Long> implements UsuarioDAO {
    
        public Usuario findUser(String login) {
            Usuario usuario = null;
            Criteria criteria = sessionFactory.getCurrentSession().createCriteria(modelo.Usuario.class, "usu");
            criteria.add(Restrictions.eq("usu.login", login));
            usuario = (Usuario) criteria.list().get(0);
            return usuario;
        }
    }
    continue in the next post under....

  • #2
    and here is the error:
    Code:
    org.hibernate.HibernateException: createCriteria is not valid without active transaction
            at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:297)
            at $Proxy32.createCriteria(Unknown Source)
            at dao.impl.UsuarioDAOHibernateImpl.findUser(UsuarioDAOHibernateImpl.java:13)
            at servicios.impl.UserDetailsServiceImpl.loadUserByUsername(UserDetailsServiceImpl.java:21)
            at org.springframework.security.providers.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:83)
            at org.springframework.security.providers.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:121)
            at org.springframework.security.providers.ProviderManager.doAuthentication(ProviderManager.java:188)
            at org.springframework.security.AbstractAuthenticationManager.authenticate(AbstractAuthenticationManager.java:46)
            at org.springframework.security.ui.webapp.AuthenticationProcessingFilter.attemptAuthentication(AuthenticationProcessingFilter.java:82)
            at org.springframework.security.ui.AbstractProcessingFilter.doFilterHttp(AbstractProcessingFilter.java:259)
            at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
            at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
            at org.springframework.security.ui.logout.LogoutFilter.doFilterHttp(LogoutFilter.java:89)
            at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
            at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
            at org.springframework.security.context.HttpSessionContextIntegrationFilter.doFilterHttp(HttpSessionContextIntegrationFilter.java:235)
            at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
            at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
            at org.springframework.security.util.FilterChainProxy.doFilter(FilterChainProxy.java:175)
            at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:183)
            at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:138)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:246)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
            at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:313)
            at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:287)
            at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:218)
            at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648)
            at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593)
            at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:94)
            at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:98)
            at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:222)
            at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648)
            at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593)
            at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:587)
            at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1096)
            at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:166)
            at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648)
            at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593)
            at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:587)
            at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1096)
            at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:288)
            at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.invokeAdapter(DefaultProcessorTask.java:647)
            at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.doProcess(DefaultProcessorTask.java:579)
            at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.process(DefaultProcessorTask.java:831)
            at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.executeProcessorTask(DefaultReadTask.java:341)
            at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:263)
            at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:214)
            at com.sun.enterprise.web.connector.grizzly.TaskBase.run(TaskBase.java:265)
            at com.sun.enterprise.web.connector.grizzly.ssl.SSLWorkerThread.run(SSLWorkerThread.java:106)
    Everything explodes in this line of the UsuarioDAOHibernateImpl:
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria( modelo.Usuario.class, "usu");

    If I put a beginTransaction() and Commit() like this I dont get the error:
    Code:
        public Usuario findUser(String login) {
            sessionFactory.getCurrentSession().getTransaction().begin();
            Usuario usuario = null;
            Criteria criteria = sessionFactory.getCurrentSession().createCriteria(modelo.Usuario.class, "usu");
            criteria.add(Restrictions.eq("usu.login", login));
            usuario = (Usuario) criteria.list().get(0);
            sessionFactory.getCurrentSession().getTransaction().commit();
            return usuario;
        }

    Comment


    • #3
      Next time please use the search, this question has been answered numerous times before.

      Code:
      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
      <hibernate-configuration>
        <session-factory>
          <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
          <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
          <property name="hibernate.connection.url">jdbc:mysql://001tec10:3306/tridopoll</property>
          <property name="hibernate.connection.username">root</property>
          <property name="hibernate.connection.password">swi966</property>
          <property name="hibernate.hbm2ddl.auto">update</property>
          <!-- Use the C3P0 connection pool.-->
          <property name="hibernate.c3p0.max_size">5</property>
          <property name="hibernate.c3p0.min_size">0</property>
          <property name="hibernate.c3p0.timeout">5000</property>
          <property name="hibernate.c3p0.max_statements">100</property>
          <property name="hibernate.c3p0.idle_test_period">300</property>
          <property name="hibernate.c3p0.acquire_increment">2</property>
          <property name="current_session_context_class">thread</property>
          <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
          <property name="cache.use_query_cache">false</property>
          <property name="cache.use_minimal_puts">false</property>
          <property name="max_fetch_depth">3</property>
          <mapping class="modelo.Usuario"/>
        </session-factory>
      </hibernate-configuration>
      REmove the red line.

      The lines in blue are useless because you have configured a datasource in the application context. I suggest moving the remaining properties to the application context as well so that your configuration is in one location.

      Comment


      • #4
        Thanks Martin! You are right, that line was the problem:
        Code:
            <property name="current_session_context_class">thread</property>
        I remove it and everything works fine. I was sure that was something dumb like that.

        Sorry for posting a problem already treated. I made a search in the forum previously but couldnīt find a similiar problem or configuration like mine.
        The next time I promise to make a deeper search!

        Thanks again.

        Comment

        Working...
        X