2014-01-31 39 views
1

我是新的春天mvc和休眠。如何關閉連接使用會話工廠

如何在春季mvc應用中關閉連接。我對這個問題感到非常沮喪。

這是我的代碼:

調度的servlet:

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

    <context:component-scan base-package="com.kqics" /> 

    <bean id="viewResolver" 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
     p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /> 

    <bean id="userService" class="com.kqics.dao.kqtraveldao"> 
    </bean> 

    <bean id="viewResolver1" class="org.springframework.web.servlet.view.ResourceBundleViewResolver"> 
     <property name="order" value="1"/> 
     <property name="basename" value="views"/> 
    </bean> 

    <bean id="multipartResolver" 
     class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 

     <!-- one of the properties available; the maximum file size in bytes --> 
     <property name="maxUploadSize" value="10000000" /> 
    </bean> 

    <import resource="db-config.xml" /> 

</beans> 

dbconfig.xml

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

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="location"><value>/WEB-INF/jdbc.properties</value></property> 
</bean> 


<bean id="dataSourceBean" lazy-init="true" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> 

     <property name="driverClass" value="${jdbc.driverClassName}" /> 
     <property name="jdbcUrl" value="${jdbc.url}" /> 
     <property name="user" value="${jdbc.username}" /> 
     <property name="password" value="${jdbc.password}"/> 


     <property name="acquireIncrement" value="${jdbc.acquireIncrement}" /> 
     <property name="minPoolSize" value="${jdbc.minPoolSize}" /> 
     <property name="maxPoolSize" value="${jdbc.maxPoolSize}" /> 
     <property name="maxIdleTime" value="${jdbc.maxIdleTime}" /> 
     <property name="numHelperThreads" value="${jdbc.numHelperThreads}" /> 


    </bean> 
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" 
       p:dataSource-ref="dataSourceBean" 
       p:packagesToScan="com.kqics" > 

     <property name="hibernateProperties"> 
     <props> 
     <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
       <!-- <prop key="hibernate.hbm2ddl.auto">create</prop> --> 
     <prop key="hibernate.show_sql">true</prop> 
     <prop key="hibernate.connection.release_mode">after_transaction</prop> 
     <prop key="hibernate.connection.shutdown">true</prop> 
     </props> 
     </property> 

    </bean> 

    <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) --> 
    <tx:annotation-driven/> 

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

    </bean> 


</beans> 

我的服務類:

@Service 
public class kqtravellogservice implements ikqtravellogservice { 

@Autowired 
ikqtraveldao iDao; 

@Transactional 
public void serviceaddnewvehicle(kqvehicle obj) { 
    // TODO Auto-generated method stub 

    iDao.addnewvehicle(obj); 

} 

@Transactional 
public List<kqvehicle> servicefetchallvehicle() { 

    return iDao.fetchallvehicle(); 
} 

@Transactional 
public void serviceaddnewvehicletariff(kqvehicletariff obj,String tariff) { 

    iDao.addnewvehicletariff(obj,tariff); 

} 

道IMPL

public class kqtraveldao implements ikqtraveldao { 

    private HibernateTemplate hibernateTemplate; 

    @Autowired 
    public void setSessionFactory(SessionFactory sessionFactory) { 
     try { 
      hibernateTemplate = new HibernateTemplate(sessionFactory); 

     } catch (Exception w) { 
     } 

    } 


    @Override 
    public void addnewvehicle(kqvehicle obj) { 


     hibernateTemplate.save(obj); 

    } 

    @SuppressWarnings("unchecked") 
    @Override 
    public List<kqvehicle> fetchallvehicle() { 

     List<kqvehicle> li=null; 

    li=hibernateTemplate.find("from kqvehicle"); 


    return li; 
    } 

    @Override 
     public void addnewvehicletariff(kqvehicletariff obj, String tariff) { 

      try 
      { 
      hibernateTemplate.getSessionFactory() 
      .openSession() 
      .createSQLQuery("insert into "+tariff+" values(?,?,?,?,?)") 
      .setParameter(0, obj.getTid()) 
      .setParameter(1, obj.getVehicletype()) 
      .setParameter(2, obj.getRupees()) 
      .setParameter(3, obj.getDateupto()) 
      .setParameter(4, obj.getDatetimedetermined()) 
      .executeUpdate(); 
      } 
      catch(Exception e) 
      { 

      } 
      finally 
      { 
       hibernateTemplate.getSessionFactory().close(); 

      } 


     } 

一些朋友,因爲我沒有使用單,關閉連接告訴我..所以,我得到了太多的連接錯誤......請諮詢我如何解決這個問題?

什麼是我的代碼需要更改....

回答

0

問題是在你的道,你的保存方法是破壞彈簧適當的tx管理。當您使用Spring來管理連接和會話時,請不要致電openSession()

取而代之的是使用HibernateCallback,它會給你一個春季管理會話。

@Override 
public void addnewvehicletariff(final kqvehicletariff obj, final String tariff) { 
    hibernateTemplate.execute(new HibernateCallback() { 
     public Object doInHibernate(Session session) { 
      session.createSQLQuery("insert into "+tariff+" values(?,?,?,?,?)") 
      .setParameter(0, obj.getTid()) 
      .setParameter(1, obj.getVehicletype()) 
      .setParameter(2, obj.getRupees()) 
      .setParameter(3, obj.getDateupto()) 
      .setParameter(4, obj.getDatetimedetermined()) 
      .executeUpdate(); 
     } 
    } 
} 

另外一個需要注意的是,你不應該使用HibernateTemplate了,你應該使用getCurrentSession()方法上SessionFactory編寫針對使用Hibernate API代碼。有關更多信息,請參閱http://docs.spring.io/spring/docs/current/spring-framework-reference/html/orm.html#orm-hibernate-straight

public class kqtraveldao implements ikqtraveldao { 

    private SessionFactory sessionFactory; 

    @Autowired 
    public void setSessionFactory(SessionFactory sessionFactory) { 
     this.sessionFactory=sessionFactory; 
    } 

    @Override 
    public void addnewvehicle(kqvehicle obj) { 
     sessionFactory.getCurrentSession().save(obj); 
    } 

    @SuppressWarnings("unchecked") 
    @Override 
    public List<kqvehicle> fetchallvehicle() { 
     return sessionFactory.getCurrentSession() 
      .createQuery("from kqvehicle") 
      .list(); 
    } 

    @Override 
    public void addnewvehicletariff(kqvehicletariff obj, String tariff) { 
     sessionFactory.getCurrentSession() 
     .createSQLQuery("insert into "+tariff+" values(?,?,?,?,?)") 
     .setParameter(0, obj.getTid()) 
     .setParameter(1, obj.getVehicletype()) 
     .setParameter(2, obj.getRupees()) 
     .setParameter(3, obj.getDateupto()) 
     .setParameter(4, obj.getDatetimedetermined()) 
     .executeUpdate(); 
    } 
} 
+0

謝謝Deinum,你能解釋一下單身對象和我們在上面代碼中使用** singleton對象**的情況。 –

+0

你的代碼已經使用了一個單例,所以不知道你想要解釋什麼。 –

+0

嗨Deinum,我已經改變了我的應用程序,如上所述,但現在仍然有類似的問題。我已經發布另一個查詢堆棧溢出請訪問。這個鏈接.. http://stackoverflow.com/questions/21520106/tomcat-server-not-loading-more-than-2-applications/21520369?noredirect=1#21520369 –

0

只是自動裝配在DAO SessionFactory並刪除方法設置會話工廠。

@Autowired 
private SessionFactory sessionfactory; 

您可以通過在您的方法中調用sessionfactory.getCurrentSession().close()來關閉連接。

Session工廠是您的應用程序中的「單例」。

相關問題