3

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

4 Answers 4

6

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

Reference:

Sign up to request clarification or add additional context in comments.

3 Comments

Ok, I've tried with apache's BasicDataSource and it works very well ! And as axtavt said, the use of the DriverManagerDataSource is discouraged exactly because of the bad connection handles. Thank you all for your responses.
@MaryRyllo glad to be of help :-)
This one saved me. Spring was assigning a lot of connections apparently. The DBCP DS uses only 1. Absolutely splendid! :-D Thanks guys!
4

It's not recommended to use DriverManagerDataSource in production, since it doesn't perform connection pooling.

You need to use connection pool instead, such as c3p0 or Apache DBCP.

Comments

2

Its better to use the connection pooling of the application server and specify the jndi in spring xml file :

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="jdbc/DSTest"/>
</bean>

Comments

0

You can use the native oracle datasource with connection caching like:

<bean id="myDataSource" class="oracle.jdbc.pool.OracleDataSource" destroy-method="close">
        <property name="connectionCachingEnabled" value="true"/>
        <property name="URL">
            <value>${jdbc.url}</value>
        </property>
        <property name="user">
            <value>${jdbc.username}</value>
        </property>
        <property name="password">
            <value>${jdbc.password}</value>
        </property>
        <property name="connectionCacheProperties">
            <value>
                MinLimit:1
                MaxLimit:1
                InitialLimit:1
                ConnectionWaitTimeout:120
                InactivityTimeout:180
                ValidateConnection:true
                MaxStatementsLimit:0
            </value>
        </property>
    </bean>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.