Announcement Announcement Module
Collapse
No announcement yet.
Pass DB connection on the fly to hibernate session for the DAO Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Pass DB connection on the fly to hibernate session for the DAO

    I am developing an application based on spring hibernate using myeclipse .
    I generated Spring DAOs using database explorer and manage to do different DB operations for testing, using a static datasource defined in the applicationContext.xml (NOT THE ONE BELOW)

    Now, for the actual application. Each user in my company has his own Oracle user. Therefore it is required to use individual user based connection for any application.
    So for each hibernate session I have to create a new oracle connection based on the individual user. Datasources and ConnectionPool does not work here.
    (Grrrrr, I know, you will not find this in normal applications but this is how our environment works.)

    I have generated DAO based on myeclipse and each DAO is extended by default by org.springframework.orm.hibernate3.support.Hiberna teDaoSupport class which uses getHibernateTemplate() method to execute database operations. Internally HibernateDaoSupport class uses the default session factory as shown in the below applicationContext.xml where I donít specify the Oracle connection details as I want to pass the connection on the fly.

    Is there a way to pass the oracle connection to hibernate session for the generated DAOs?

    Code:
    <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"
          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
    
          <bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
                
                <property name="hibernateProperties">
                      <props>
                            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                      </props>
                </property>
                <property name="mappingResources">
                <list>
                  <value>com/aramco/peasd/springHibernate/objects/RtocEngrRqstTrouble.hbm.xml</value>
                  <value>com/aramco/peasd/springHibernate/objects/RtocEngrRqst.hbm.xml</value>
                </list>
              </property>
                
          </bean>
          
          <bean id="RtocEngrRqstTroubleDAO" class="com.aramco.peasd.springHibernate.objects.RtocEngrRqstTroubleDAO">
                <property name="sessionFactory">
                      <ref bean="hibernateSessionFactory" />
                </property>
          </bean>
          
          <bean id="RtocEngrRqstDAO" class="com.aramco.peasd.springHibernate.objects.RtocEngrRqstDAO">
                <property name="sessionFactory">
                      <ref bean="hibernateSessionFactory" />
                </property>
          </bean>
    
    </beans>

  • #2
    Please use the search as this question has been answered numerous times. I suggest taking a look at the UserCredentialsDataSourceAdapter and search the forum for it. Basically, set the username/password on the start of the request, and clear it after it has finished.

    Another note it isn't recommended to use HibernateDaoSupport and/or HibernateTemplate anymore that is all explained in the reference guide.

    Comment


    • #3
      Works fine

      Thanks a million Marten, UserCredentialsDataSourceAdapter works fine.
      I used MyEclipse 9, to generate SpringDAOs which by default used HibernateDaoSupport.
      Will surely look into the option other than HibernateDaoSupport .

      Comment


      • #4
        Originally posted by ammarsit View Post
        Thanks a million Marten, UserCredentialsDataSourceAdapter works fine.
        I used MyEclipse 9, to generate SpringDAOs which by default used HibernateDaoSupport.
        Will surely look into the option other than HibernateDaoSupport .
        Hi Ammarsit,

        I'm facing the same requirements as you have. I have implemented UserCredentialsDataSourceAdapter and it change the connection but the sessionFactory still remain using the same connections.

        Can you please give me an idea how to implement in order to make it works.

        Regards,

        Erawat

        Comment


        • #5
          Using UserCredentialsDataSourceAdapter for Test and in the web applications.

          First you have to update the applicationContext.xml. No need to pass username and password, as you can see that I have commented it out.

          Code:
          	<bean id="targetDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
              	<property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property>
              	<property name="url"><value>jdbc:oracle:oci:@DBNAME</value></property>
            	</bean>
            	
            	<bean id="dataSource" class="org.springframework.jdbc.datasource.UserCredentialsDataSourceAdapter">
          	   <property name="targetDataSource"><ref bean="targetDataSource"/></property>
          <!--	   <property name="username"><value></value></property>-->
          <!--	   <property name="password"><value></value></property>-->
          	</bean>
          Next within the application your have to set the credentials and remove them once you are done. Please see that I am using the "datasource" which is the ID for UserCredentialsDataSourceAdapter in applicationContext.xml

          Code:
          ApplicationContext appContext = new ClassPathXmlApplicationContext("APP_CONTEXT")
          UserCredentialsDataSourceAdapter ds = (UserCredentialsDataSourceAdapter)appContext .getBean("dataSource");
          
          ds.setCredentialsForCurrentThread(USER_NAME, PASSWORD);
          
          // DO YOU PROCESSION HERE. 
          
          ds..removeCredentialsFromCurrentThread();
          For web applications use the same code in Servlet Filter doFilter() method. Following is the code for it.

          Code:
          ds.setCredentialsForCurrentThread(USER_NAME, PASSWORD);
          
          chain.doFilter(); // do processing and while returning back remove the credentials. 
          
          ds..removeCredentialsFromCurrentThread();
          I hope this is helpful.

          Regads

          Comment

          Working...
          X