Announcement Announcement Module
Collapse
No announcement yet.
help me about spring + hibernate ,my friends ,thank you Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • help me about spring + hibernate ,my friends ,thank you

    hi,all my dear friends :
    I have two question about spring + hibernate:
    1- warn 2005-12-02 09:06:06,812 WARN [org.hibernate.impl.SessionImpl :1728] - unclosed connection
    2-no session or session was closed exception org.apache.jasper.JasperException: Failed to lazily initialize a collection - no session or session was closed

    I come from China ,and very vexation about this question ,my dear friends can you help me ?thank You !!

    attachment is my config file

    1-applicationContext.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

    <beans>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean" singleton="true">
    <property name="configLocation">
    <value>classpath:hibernate.cfg.xml</value>
    </property>

    </bean>

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

    <bean id="hibernateDao" class="com.task.common.HibernateDao">
    <property name="sessionFactory">
    <ref local="sessionFactory" />
    </property>
    <property name="transactionManager">
    <ref local="transactionManager" />
    </property>
    </bean>

    <bean id="daoManager" singleton="true" class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
    <property name="transactionManager">
    <ref bean="transactionManager" />
    </property>

    <property name="target">
    <ref local="hibernateDao" />
    </property>

    <property name="transactionAttributes">
    <props>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    <prop key="del*">PROPAGATION_REQUIRED</prop>
    <prop key="update*">PROPAGATION_REQUIRED</prop>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
    </props>
    </property>
    </bean>

    </beans>

    2-web.xml
    <?xml version="1.0" encoding="ISO-8859-1"?>

    <!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">

    <web-app>

    <filter>
    <filter-name>openSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.O penSessionInViewFilter</filter-class>
    <init-param>
    <param-name>singleSession</param-name>
    <param-value>false</param-value>
    </init-param>
    </filter>
    <filter-mapping>
    <filter-name>openSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>

    <servlet>
    <servlet-name>context</servlet-name>
    <servlet-class>org.springframework.web.context.ContextLoade rServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>

    <listener>
    <listener-class>org.springframework.web.context.ContextLoade rListener</listener-class>
    </listener>

    <filter>
    <filter-name>webinit</filter-name>
    <filter-class>com.task.common.ServerFilter</filter-class>
    </filter>
    <filter-mapping>
    <filter-name>webinit</filter-name>
    <url-pattern>*.*</url-pattern>
    </filter-mapping>

    <servlet>
    <servlet-name>task</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherSe rvlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
    <servlet-name>task</servlet-name>
    <url-pattern>*.job</url-pattern>
    </servlet-mapping>

    <servlet>
    <servlet-name>myTest</servlet-name>
    <servlet-class>com.task.test.TestServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
    <servlet-name>myTest</servlet-name>
    <url-pattern>*.test</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    </web-app>

    3-HibernateDao.java

    package com.task.common;

    import java.util.List;

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.hibernate.Query;
    import org.hibernate.Session;
    import org.springframework.orm.ObjectRetrievalFailureExce ption;
    import org.springframework.orm.hibernate3.support.Hiberna teDaoSupport;
    import org.springframework.transaction.PlatformTransactio nManager;

    /**
    * @author xurun dao
    */
    public class HibernateDao extends HibernateDaoSupport{
    private PlatformTransactionManager transactionManager;

    private Log log = LogFactory.getLog(HibernateDao.class);

    /** 根据hql查询所有实体 */
    public List getAllEntityByHql(String hql) {
    List list = getHibernateTemplate().find(hql);
    return list;
    }

    public List getEntityByHqlAndStartRecord(String hql, int startRecord) {
    return getEntityByHqlAndStartRecordLength(hql, startRecord, -1);
    }

    public List getEntityByHqlAndStartRecordLength(String hql, int startRecord,
    int recordsPerPage) {

    if (recordsPerPage == -1) {
    recordsPerPage = 20; // 默认显示20行*/
    }

    Session session = getHibernateTemplate().getSessionFactory().openSes sion();

    Query query = session.createQuery(hql);
    query.setFirstResult(startRecord);
    query.setMaxResults(recordsPerPage);

    session.close();
    return query.list();
    }

    public Object getEntityById(Class entityClass, Integer id) {

    Object obj = getHibernateTemplate().get(entityClass, id);
    if (obj == null) {
    throw new ObjectRetrievalFailureException(entityClass, id);
    }
    return obj;
    }

    public void saveEntity(Object entity) {

    getHibernateTemplate().save(entity);
    }

    public void updateEntity(Object entity) {

    getHibernateTemplate().update(entity);
    }

    public void saveOrUpdateEntity(Object entity) {

    getHibernateTemplate().saveOrUpdate(entity);
    }

    public void removeEntity(Object entity) {
    getHibernateTemplate().delete(entity);

    }

    public void removeEntity(Class entityClass, Integer id) {

    Object obj = getHibernateTemplate().load(entityClass, id);

    getHibernateTemplate().delete(obj);

    }

    public List getLoadUserEntityByHql(String hql) {
    return getHibernateTemplate().find(hql);
    }


    public PlatformTransactionManager getTransactionManager() {
    return transactionManager;
    }


    public void setTransactionManager(
    PlatformTransactionManager transactionManager) {
    this.transactionManager = transactionManager;
    }
    }

    4-ServerFilter.java
    package com.task.common;

    import java.io.IOException;

    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.web.context.WebApplicationCont ext;
    import org.springframework.web.context.support.WebApplica tionContextUtils;


    public class ServerFilter implements Filter {
    private static Log log = LogFactory.getLog(ServerFilter.class);

    public void destroy() {
    }

    public void doFilter(
    ServletRequest request,
    ServletResponse response,
    FilterChain chain)
    throws IOException, ServletException {
    try{
    chain.doFilter( request, response );
    } finally {

    request.setCharacterEncoding("GBK");
    }
    }

    public void init(FilterConfig filterConfig) throws ServletException {
    ServletContext sc = filterConfig.getServletContext();
    WebApplicationContext wct = WebApplicationContextUtils.getWebApplicationContex t(sc);

    log.debug("system information initialization ... ");
    ServiceProvider sp = ServiceProvider.getInstance();
    sp.setApplicationContext(wct); //WebApplicationContext stay memory

    }
    }
    Last edited by kxurun; Jan 12th, 2006, 03:23 AM.
Working...
X