Announcement Announcement Module
Collapse
No announcement yet.
Problem with management of the spring session Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with management of the spring session

    hi guys, I have problem in spring in time to save and delete objects in the database, the web application crashes no longer carries the lists

    please help me.....

    aplicationContext
    <?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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/tx http://www.springframework.org/schem...ing-tx-3.0.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean id="databaseProperties"
    class="org.springframework.beans.factory.config.Pr opertyPlaceholderConfigurer">
    <property name="location" value="/WEB-INF/database.properties" />
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    </bean>

    <!-- DATA SOURCE -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${db.driver}" />
    <property name="url" value="${db.url}" />
    <property name="username" value="${db.username}" />
    <property name="password" value="${db.password}" />
    </bean>

    <!-- SESSION FACTORY -->
    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotati on.AnnotationSessionFactoryBean">
    <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
    <property name="annotatedClasses">
    <list>

    <value>model.HerbNativa</value>

    </list>
    </property>
    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
    <props>
    <!-- Dialeto -->
    <!-- <prop key="hibernate.hbm2ddl.auto">update</prop>-->
    <prop key="hibernate.dialect">${db.dialect}</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.generate_statistics">true</prop>
    <prop key="format_sql">true</prop>
    <prop key="hibernate.cglib.use_reflection_optimizer">fal se</prop>
    </props>
    </property>
    </bean>

    <!-- TRANSACTION MANAGER -->
    <bean id="transactionManager"
    class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <!-- ANNOTATION DRIVEN TRANSACTIONS -->
    <tx:annotation-driven transaction-manager="transactionManager" />

    <context:annotation-config />

    </beans>




    GenericDAO

    package dao;

    import java.sql.Connection;
    import java.sql.SQLException;
    import java.util.List;

    public interface GenericDAO <T> {
    public Connection getConnection() throws SQLException;
    public T save(T object);
    public void delete(T object);
    public T getById(final Long id);
    public List<T> listAll();
    }


    GenericDAOImpl

    package br.com.systhemis.co2geo.dao.impl;

    import java.sql.Connection;
    import java.sql.SQLException;
    import java.util.List;
    import javax.sql.DataSource;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.hibernate.Criteria;
    import org.hibernate.HibernateException;
    import org.hibernate.Query;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.springframework.beans.factory.annotation.Autow ired;
    import org.springframework.beans.factory.annotation.Quali fier;
    import org.springframework.orm.hibernate3.support.Hiberna teDaoSupport;
    import org.springframework.transaction.annotation.Transac tional;

    import dao.GenericDAO;

    @Transactional
    public class GenericDAOImpl<T> extends HibernateDaoSupport implements GenericDAO<T> {

    private static final long serialVersionUID = 1L;
    private static final Log logger = LogFactory.getLog(GenericDAOImpl.class);
    private final Class<?> objectClass;

    private DataSource dataSource;

    public DataSource getDataSource() {
    return dataSource;
    }
    @Autowired
    public void setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;
    }

    public GenericDAOImpl(final Class<?> objectClass) {
    this.objectClass = objectClass;
    }

    public Connection getConnection() throws SQLException {
    return dataSource.getConnection();
    }

    @Autowired
    public void SessionFactory(@Qualifier("sessionFactory")Session Factory factory) {
    setSessionFactory(factory);
    }

    public T save(T object) {
    try {
    final Session session = getSession();
    session.saveOrUpdate(object);
    return object;
    } catch (final HibernateException ex) {
    GenericDAOImpl.logger.error(ex);
    throw convertHibernateAccessException(ex);
    }
    }

    @SuppressWarnings("unchecked")
    public T getById( final Long id ) {
    try {
    final Session session = getSession();
    return (T) session.get( objectClass, id );

    } catch (HibernateException ex) {
    GenericDAOImpl.logger.error(ex);
    throw convertHibernateAccessException(ex);
    }
    }

    @SuppressWarnings("unchecked")
    protected List<T> find(String hql){
    try{
    Query query = getSession().createQuery(hql);
    return query.list();
    }catch(Throwable e){
    System.out.println("Erro ao executar HQL - "+hql);
    e.printStackTrace();
    return null;
    }

    }

    @SuppressWarnings("unchecked")
    public List<T> listAll() {
    try {
    final Session s = getSession();
    final Criteria c = s.createCriteria(objectClass);
    return c.list();
    } catch (final HibernateException ex) {
    GenericDAOImpl.logger.error(ex);
    throw convertHibernateAccessException(ex);
    }
    }

    public void delete(T object) {

    try {
    final Session session = getSession();
    session.delete(object);
    session.flush();
    } catch (final HibernateException ex) {
    GenericDAOImpl.logger.error(ex);
    throw convertHibernateAccessException(ex);
    }
    }
    }

  • #2
    Please use [ code][/code ] tags when posting code. Not sure what your exact issue is ( no stracktrace, no error attached) we need to guess, but your implementation has a couple of issues.

    1) Don't use hibernatetemplate/hibernatedaosupport it isn't recommended anymore (for about 3 years)
    2) Don't use getSession
    3) use the sessionfactory directy and use getCurrentSession on that
    4) Don't catch exceptions, spring does the conversion for you

    Comment


    • #3
      then, my application does not generate any error, just hangs, I'm sure is because of the session, thanks I'll try to change my methods.

      Comment


      • #4
        I tried to change the methods of getssession to getCurrentSession yet my application still hangs when making several saves, or delete, anyone have any idea?

        Comment

        Working...
        X