与HibernateTemplate和SessionFactory的单一数据库连接 [英] Single database connection with HibernateTemplate and SessionFactory

查看:92
本文介绍了与HibernateTemplate和SessionFactory的单一数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在java方面,一切正常,但是当我查看V $ SESSION特殊的oracle表,并在我的日志表中记录任何登录或注销操作时,这是一场灾难......每一个查询都会执行登录/注销操作。所以,这里是我的问题:有什么办法可以配置Spring与数据库有唯一的连接,或者在连接的方式上有什么问题?
在这里,我的数据源bean配置:

$ p $ < bean id =dataSource
class = org.springframework.jdbc.datasource.DriverManagerDataSource>
< property name =driverClassName>
< value> oracle.jdbc.OracleDriver< /值>
< / property>
< property name =url>
< value> jdbc:oracle:thin:@nanssunorad:1523:nanorad3< / value>
< / property>
< property name =username>
< value> foo< /值>
< / property>
< property name =password>
<值>栏< /值>
< / property>
< / bean>
< bean id =sessionFactory
class =org.springframework.orm.hibernate3.LocalSessionFactoryBean>
< property name =configLocation>
<值> hibernateESign.cfg.xml< /值>
< / property>
< property name =dataSource>
< ref bean =dataSource/>
< / property>
< property name =configurationClass>
< value> org.hibernate.cfg.AnnotationConfiguration< / value>
< / property>
< / bean>

< bean id =transactionManager
class =org.springframework.orm.hibernate3.HibernateTransactionManager>
< property name =sessionFactory>
< ref local =sessionFactory/>
< / property>
< / bean>
< bean id =IXalVaParametresDAOclass =fr.asterion.archivage.hibernate.XalVaParametresDAO>
< property name =sessionFactory>
< ref local =sessionFactory/>
< / property>
< / bean>

在我的应用程序中,为了获得数据库参数的DAO,我做了一个

  IXalVaParametresDAO parametreDAO =(IXalVaParametresDAO)ConfigApplication 
.getApplicationContext(this.log).getBean(IXalVaParametresDAO);

最后,在我的DAO类中,我做了这样的工作:

  public class XalVaParametresDAO implements IXalVaParametresDAO 
{

private HibernateTemplate hibernateTemplate;

public void setSessionFactory(SessionFactory sessionFactory)
{
this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}



public List< XalVaParametres> findAll()
{
log.debug(查找所有XalVaParametres实例);
尝试
{
字符串queryString =from XalVaParametres;
List lst = this.hibernateTemplate.find(queryString);

在我的应用程序中,每次我调用find方法时,都会执行数据库登录/注销。我猜这个问题在另一个数据库上是一样的。我正确地做事情吗?我猜不会。我希望HibernateTemplate能创建并保持会话一劳永逸。那可能吗 ?
感谢您的建议



Manux

解决方案

使用合并 DataSource ,例如 Apache Commons / DBCP BasicDataSource



参考:


on the java side, everything works fine but when I look to the V$SESSION special oracle table , and in my log table that records any login or logoff operations, that's a disaster... every single query does a login/logoff operation. So here's my question : Is there any way to configure Spring to have a unique connection to the database or is there something wrong in the way I do the connection ? Here, my datasource bean configuration :

<bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName">
            <value>oracle.jdbc.OracleDriver</value>
        </property>
        <property name="url">
            <value>jdbc:oracle:thin:@nanssunorad:1523:nanorad3</value>
        </property>
        <property name="username">
            <value>foo</value>
        </property>
        <property name="password">
            <value>bar</value>
        </property>
    </bean>
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="configLocation">
        <value>hibernateESign.cfg.xml</value>
    </property>
    <property name="dataSource">
        <ref bean="dataSource" />
    </property>
    <property name="configurationClass">
        <value>org.hibernate.cfg.AnnotationConfiguration</value>
    </property>
</bean>

<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
        <ref local="sessionFactory" />
    </property>
</bean>
<bean id="IXalVaParametresDAO" class="fr.asterion.archivage.hibernate.XalVaParametresDAO">
    <property name="sessionFactory">
        <ref local="sessionFactory" />
    </property>
</bean>

In my app, to get the DAO for the database parameters, I do a

  IXalVaParametresDAO parametreDAO = (IXalVaParametresDAO) ConfigApplication
           .getApplicationContext(this.log).getBean("IXalVaParametresDAO");

And at last, in my DAO class, I do someting like this :

public class XalVaParametresDAO implements IXalVaParametresDAO
{

   private HibernateTemplate hibernateTemplate;

   public void setSessionFactory(SessionFactory sessionFactory)
   {
      this.hibernateTemplate = new HibernateTemplate(sessionFactory);
   }



   public List<XalVaParametres> findAll()
   {
      log.debug("finding all XalVaParametres instances");
      try
      {
         String queryString = "from XalVaParametres";
         List lst = this.hibernateTemplate.find(queryString);

In my app, each time I call the "find" method, it does a DB login/logoff. I guess the problem is the same on another DB. Am I doing the things right ? I guess not. I wish the HibernateTemplate would create and keep the session open once and for all. Is that possible ? Thanks for your ideas

Manux

解决方案

Use a pooling DataSource, e.g. Apache Commons / DBCP's BasicDataSource

Reference:

这篇关于与HibernateTemplate和SessionFactory的单一数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆