微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – 如何解决当前线程找不到会话

我试图做通用的DAO的实现方式,我按照 Article

以下是我的genericDaoImpl类

@SuppressWarnings("unchecked")
@Repository
public abstract class GenericDaoImpl<E,K extends Serializable> 
        implements GenericDao<E,K> {
    @Autowired
    private SessionFactory sessionFactory;

    protected Class<? extends E> daoType;

    /**
    * By defining this class as abstract,we prevent Spring from creating 
    * instance of this class If not defined as abstract,* getClass().getGenericSuperClass() would return Object. There would be 
    * exception because Object class does not hava constructor with parameters.
    */
    public GenericDaoImpl() {
        Type t = getClass().getGenericSuperclass();
        ParameterizedType pt = (ParameterizedType) t;
        daoType = (Class) pt.getActualTypeArguments()[0];
    }

    protected Session currentSession() {
        return sessionFactory.getCurrentSession();
    }

    @Override
    public void add(E entity) {
        currentSession().save(entity);
    }

    @Override
    public void saveOrUpdate(E entity) {
        currentSession().saveOrUpdate(entity);
    }

    @Override
    public void update(E entity) {
        currentSession().saveOrUpdate(entity);
    }

    @Override
    public void remove(E entity) {
        currentSession().delete(entity);
    }

    @Override
    public E find(K key) {
        return (E) currentSession().get(daoType,key);
    }

    @Override
    public List<E> getAll() {
        return currentSession().createCriteria(daoType).list();
    }
}

GENERICDAO

public interface GenericDao<E,K> {
    public void add(E entity) ;
    public void saveOrUpdate(E entity) ;
    public void update(E entity) ;
    public void remove(E entity);
    public E find(K key);
    public List<E> getAll() ;
}

服务类

@Service
public class test {
    @Autowired
    TestPlanDao testPlanDao;
    @Transactional(propagation = Propagation.required)
    public int saveTestPlan()
    {

        try
        {

        TestPlan tp=new TestPlan();

        tp.setTestplan_version(1);
        testPlanDao.saveTestPlan(tp);
        logger.info("testplan saved");
        return 1;
        }
        catch(Exception e)
        {
            e.printstacktrace();
            logger.error(e.getMessage(),e);
            return 0;
        }

    }

这是我的daoImpl

@Repository
public class TestPlanDaoImpl extends GenericDaoImpl<TestPlan,Integer> implements TestPlanDao{



    @Override
    @Transactional
    public void saveTestPlan(TestPlan tp) {
        // Todo Auto-generated method stub
        add(tp);

    }

hibernate配置xml

<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://${MysqLHost}/${MysqLdatabase}" />

        <property name="username" value="${MysqLUserName}" />
        <property name="password" value="${MysqLPassword}" />
        <property name="removeAbandoned" value="true" />
        <property name="initialSize" value="20" />
        <property name="maxActive" value="30" />
        <property name="maxIdle" value="-1" />
        <property name ="testOnBorrow" value="true"/>
        <property name ="validationQuery" value="SELECT 1"/>
    </bean>

  <bean id="sessionFactoryConf"
        class="org.springframework.orm.hibernate4.LocalSessionfactorybean">
        <property name="dataSource" ref="dataSource" />
        <property name="annotatedClasses">
            <list>

                    <value>com.test.model.TestPlan</value>

                </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MysqLDialect</prop>

                <prop key="hibernate.transaction.auto_close_session">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
  </bean>

我不能找到原因

org.hibernate.HibernateException: No Session found for current thread
    at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:106)
    at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:988)

解决方法

尝试删除以下属性
<prop key="hibernate.transaction.auto_close_session">true</prop>

我相信Hibernate会很快关闭会话,导致你的错误.由于您使用Spring TransactionManager,请关闭会话.

原文地址:https://www.jb51.cc/java/124133.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐