Results 1 to 10 of 10

Thread: use of Spring/Hibernate with two database user ??

  1. #1
    Join Date
    Oct 2005
    Posts
    8

    Default use of Spring/Hibernate with two database user ??

    Hello,

    I'm trying to use Spring framework with Hibernate. Up until now I am using the HibernateTemplate to call Hibernate methods, and this works perfectly. I need now to have two user of oracle on one database,
    when guest access my site, I use the low privilege user to access database,
    when Administrator access my site, I use the hight privilege user to access database.

    How could I use HibernateTemplate then? could i use a sessionfactory and create sessions using different connections?
    thanks a lot

  2. #2
    Join Date
    Aug 2004
    Location
    New York
    Posts
    168

    Default

    Can you define two session factories, each pointing to a different data source with a different user name defined?
    Karl Baum
    weblog: www.jroller.com/page/kbaum

  3. #3
    Join Date
    Oct 2005
    Posts
    8

    Default how to use with the DAO???

    Quote Originally Posted by kbaum
    Can you define two session factories, each pointing to a different data source with a different user name defined?

    How to use with the DAO???each dao set two sessionFactory???

    How to use with eht transactionManager ???

  4. #4
    Join Date
    Oct 2005
    Posts
    8

    Default How to change my applicationContext.xml file

    Quote Originally Posted by kbaum
    Can you define two session factories, each pointing to a different data source with a different user name defined?
    How to change my applicationContext.xml file???
    my applicationContext.xml is below:

    <?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="dataSource1" class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
    <property name="driverClassName"><value>oracle.jdbc.driver.O racleDriver</value></property>
    <property name="url"><value>jdbc:oracle:thin:@192.168.1.2:15 21:orcl</value></property>
    <property name="username"><value>user1</value></property>
    <property name="password"><value>123456</value></property>
    </bean>

    <bean id="dataSource2" class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
    <property name="driverClassName"><value>oracle.jdbc.driver.O racleDriver</value></property>
    <property name="url"><value>jdbc:oracle:thin:@192.168.1.2:15 21:orcl</value></property>
    <property name="username"><value>user2</value></property>
    <property name="password"><value>123456</value></property>
    </bean>

    <!-- Hibernate SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSess ionFactoryBean">
    <property name="dataSource"><ref local="dataSource1"/></property>
    <property name="mappingResources">
    <list>
    <value>org/appfuse/model/User.hbm.xml</value>
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">net.sf.hibernate.dialect.H SQLDialect</prop>
    <prop key="hibernate.hbm2ddl.auto">update</prop>
    </props>
    </property>
    </bean>

    <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate.Hibernate TransactionManager">
    <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>

    <bean id="userDAO" class="org.appfuse.dao.hibernate.UserDAOHibernate" >
    <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>

    <bean id="userManagerTarget" class="org.appfuse.service.impl.UserManagerImpl">
    <property name="userDAO"><ref local="userDAO"/></property>
    </bean>

    <bean id="userManager"
    class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
    <property name="transactionManager"><ref local="transactionManager"/></property>
    <property name="target"><ref local="userManagerTarget"/></property>
    <property name="transactionAttributes">
    <props>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    <prop key="remove*">PROPAGATION_REQUIRED</prop>
    <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
    </props>
    </property>
    </bean>

    </beans>

  5. #5
    Join Date
    Aug 2004
    Location
    New York
    Posts
    168

    Default

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
    
    <beans>
    
    <bean id="dataSource1" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property>
        <property name="url"><value>jdbc&#58;oracle&#58;thin&#58;@192.168.1.2&#58;1521&#58;orcl</value></property>
        <property name="username"><value>user1</value></property>
        <property name="password"><value>123456</value></property>
    </bean>
    
    <bean id="dataSource2" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property>
        <property name="url"><value>jdbc&#58;oracle&#58;thin&#58;@192.168.1.2&#58;1521&#58;orcl</value></property>
        <property name="username"><value>user2</value></property>
        <property name="password"><value>123456</value></property>
    </bean>
    
    <!-- Hibernate SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
        <property name="dataSource"><ref local="dataSource1"/></property>
        <property name="mappingResources">
            <list>
                <value>org/appfuse/model/User.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">net.sf.hibernate.dialect.HSQLDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>
    
    <!-- Hibernate SessionFactory -->
    <bean id="sessionFactory2" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
        <property name="dataSource"><ref local="dataSource2"/></property>
        <property name="mappingResources">
            <list>
                <value>org/appfuse/model/User.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">net.sf.hibernate.dialect.HSQLDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>
    
    <!-- Transaction manager for a single Hibernate SessionFactory &#40;alternative to JTA&#41; -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
        <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>
    
    
    <bean id="transactionManager2" class="org.springframework.orm.hibernate.HibernateTransactionManager">
        <property name="sessionFactory2"><ref local="sessionFactory"/></property>
    </bean>
    
    <bean id="userDAO" class="org.appfuse.dao.hibernate.UserDAOHibernate">
         <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>
    
    
    <bean id="userDAO2" class="org.appfuse.dao.hibernate.UserDAOHibernate">
         <property name="sessionFactory"><ref local="sessionFactory2"/></property>
    </bean>
    
    <bean id="userManagerTarget" class="org.appfuse.service.impl.UserManagerImpl">
        <property name="userDAO"><ref local="userDAO"/></property>
    </bean>
    
    
    <bean id="userManagerTarget2" class="org.appfuse.service.impl.UserManagerImpl">
        <property name="userDAO"><ref local="userDAO2"/></property>
    </bean>
    
    <bean id="userManager"
    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionManager"><ref local="transactionManager"/></property>
        <property name="target"><ref local="userManagerTarget"/></property>
        <property name="transactionAttributes">
        <props>
            <prop key="save*">PROPAGATION_REQUIRED</prop>
            <prop key="remove*">PROPAGATION_REQUIRED</prop>
            <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
        </props>
    </property>
    </bean>
    
    <bean id="userManager2"
    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionManager2"><ref local="transactionManager"/></property>
        <property name="target"><ref local="userManagerTarget"/></property>
        <property name="transactionAttributes">
        <props>
            <prop key="save*">PROPAGATION_REQUIRED</prop>
            <prop key="remove*">PROPAGATION_REQUIRED</prop>
            <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
        </props>
    </property>
    </bean>
    
    </beans>
    Much of this could be factored out using abstract parent bean definitions, especially with the TransactionProxyFactoryBean datasource definitions.

    http://static.springframework.org/sp...an-definitions
    Karl Baum
    weblog: www.jroller.com/page/kbaum

  6. #6
    Join Date
    Oct 2005
    Posts
    8

    Default

    Quote Originally Posted by kbaum
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
    
    <beans>
    
    <bean id="dataSource1" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property>
        <property name="url"><value>jdbc&#58;oracle&#58;thin&#58;@192.168.1.2&#58;1521&#58;orcl</value></property>
        <property name="username"><value>user1</value></property>
        <property name="password"><value>123456</value></property>
    </bean>
    
    <bean id="dataSource2" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property>
        <property name="url"><value>jdbc&#58;oracle&#58;thin&#58;@192.168.1.2&#58;1521&#58;orcl</value></property>
        <property name="username"><value>user2</value></property>
        <property name="password"><value>123456</value></property>
    </bean>
    
    <!-- Hibernate SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
        <property name="dataSource"><ref local="dataSource1"/></property>
        <property name="mappingResources">
            <list>
                <value>org/appfuse/model/User.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">net.sf.hibernate.dialect.HSQLDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>
    
    <!-- Hibernate SessionFactory -->
    <bean id="sessionFactory2" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
        <property name="dataSource"><ref local="dataSource2"/></property>
        <property name="mappingResources">
            <list>
                <value>org/appfuse/model/User.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">net.sf.hibernate.dialect.HSQLDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>
    
    <!-- Transaction manager for a single Hibernate SessionFactory &#40;alternative to JTA&#41; -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
        <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>
    
    
    <bean id="transactionManager2" class="org.springframework.orm.hibernate.HibernateTransactionManager">
        <property name="sessionFactory2"><ref local="sessionFactory"/></property>
    </bean>
    
    <bean id="userDAO" class="org.appfuse.dao.hibernate.UserDAOHibernate">
         <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>
    
    
    <bean id="userDAO2" class="org.appfuse.dao.hibernate.UserDAOHibernate">
         <property name="sessionFactory"><ref local="sessionFactory2"/></property>
    </bean>
    
    <bean id="userManagerTarget" class="org.appfuse.service.impl.UserManagerImpl">
        <property name="userDAO"><ref local="userDAO"/></property>
    </bean>
    
    
    <bean id="userManagerTarget2" class="org.appfuse.service.impl.UserManagerImpl">
        <property name="userDAO"><ref local="userDAO2"/></property>
    </bean>
    
    <bean id="userManager"
    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionManager"><ref local="transactionManager"/></property>
        <property name="target"><ref local="userManagerTarget"/></property>
        <property name="transactionAttributes">
        <props>
            <prop key="save*">PROPAGATION_REQUIRED</prop>
            <prop key="remove*">PROPAGATION_REQUIRED</prop>
            <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
        </props>
    </property>
    </bean>
    
    <bean id="userManager2"
    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionManager2"><ref local="transactionManager"/></property>
        <property name="target"><ref local="userManagerTarget"/></property>
        <property name="transactionAttributes">
        <props>
            <prop key="save*">PROPAGATION_REQUIRED</prop>
            <prop key="remove*">PROPAGATION_REQUIRED</prop>
            <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
        </props>
    </property>
    </bean>
    
    </beans>
    Much of this could be factored out using abstract parent bean definitions, especially with the TransactionProxyFactoryBean datasource definitions.

    http://static.springframework.org/sp...an-definitions

    If acts according to making which you said,
    It's too much complex, too much code should be involved,
    Is there a easy way???

  7. #7
    Join Date
    Aug 2004
    Location
    New York
    Posts
    168

    Default

    If I extended parent bean definitions, I could have:

    Code:
    <beans>
        
        <bean id="dataSource1"
            class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName">
                <value>oracle.jdbc.driver.OracleDriver</value></property>
            <property name="url">
                <value>jdbc&#58;oracle&#58;thin&#58;@192.168.1.2&#58;1521&#58;orcl</value>
            </property>
            <property name="username"><value>user1</value></property>
            <property name="password"><value>123456</value></property>
        </bean>
        
        <bean id="dataSource2" parent="dataSource1">
            <property name="username" value="user2"/>
            <property name="password" value="123456"/>
        </bean>
        
        <!-- Hibernate SessionFactory -->
        <bean id="sessionFactory"
            class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
            <property name="dataSource"><ref local="dataSource1"/></property>
            <property name="mappingResources">
                <list>
                    <value>org/appfuse/model/User.hbm.xml</value>
                </list>
            </property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">
                        net.sf.hibernate.dialect.HSQLDialect</prop>
                    <prop key="hibernate.hbm2ddl.auto">update</prop>
                </props>
            </property>
        </bean>
        
        <!-- Hibernate SessionFactory -->
        <bean id="sessionFactory2" parent="sessionFactory">
            <property name="dataSource" ref="dataSource2"/>
        </bean>
        
        
        <!-- Transaction manager for a single Hibernate SessionFactory &#40;alternative to JTA&#41; -->
        <bean id="transactionManager"
            class="org.springframework.orm.hibernate.HibernateTransactionManager">
            <property name="sessionFactory"><ref local="sessionFactory"/></property>
        </bean>
        
        <bean id="transactionManager2"
            class="org.springframework.orm.hibernate.HibernateTransactionManager">
            <property name="sessionFactory"><ref local="sessionFactory2"/></property>
        </bean>
        
        <bean id="userDAO" class="org.appfuse.dao.hibernate.UserDAOHibernate">
            <property name="sessionFactory"><ref local="sessionFactory"/></property>
        </bean>
        
        <bean id="userDAO2" class="org.appfuse.dao.hibernate.UserDAOHibernate">
            <property name="sessionFactory"><ref local="sessionFactory2"/></property>
        </bean>
        
        <bean id="userManagerTarget" class="org.appfuse.service.impl.UserManagerImpl">
            <property name="userDAO"><ref local="userDAO"/></property>
        </bean>
        
        <bean id="userManagerTarget2"
            class="org.appfuse.service.impl.UserManagerImpl">
            <property name="userDAO"><ref local="userDAO2"/></property>
        </bean>
        
        <bean id="userManager"
            class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
            <property name="transactionManager"><ref local="transactionManager"/>
                </property>
            <property name="target"><ref local="userManagerTarget"/></property>
            <property name="transactionAttributes">
                <props>
                    <prop key="save*">PROPAGATION_REQUIRED</prop>
                    <prop key="remove*">PROPAGATION_REQUIRED</prop>
                    <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
                </props>
            </property>
        </bean>
        
        <bean id="userManager2" parent="userManager">
            <property name="transactionManager" ref="transactionManager2"/>
            <property name="target" ref="userManagerTarget2"/>
        </bean>
        
    </beans>
    Karl Baum
    weblog: www.jroller.com/page/kbaum

  8. #8
    Join Date
    Oct 2005
    Posts
    8

    Default To kbaum:

    To kbaum: Thank you very much!!!
    It's a good idea.

    but can I change the user name at runtime??
    I only want to change the user name,
    Is there any way to get and change the database user name at runtime ?

  9. #9
    Join Date
    Aug 2004
    Location
    New York
    Posts
    168

    Default

    I guess there could be a way, but I wouldn't recommend doing it. The data source is a shared singleton object. It's not thread safe to change it's state.
    Karl Baum
    weblog: www.jroller.com/page/kbaum

  10. #10
    Join Date
    Oct 2005
    Posts
    8

    Default who have other solution?

    who have other solution?

Similar Threads

  1. Problem with HibernateInterceptor
    By prane in forum Data
    Replies: 5
    Last Post: Oct 16th, 2007, 08:01 AM
  2. LDAPPasswordAuthenticationDao problem
    By benoit_m35 in forum Security
    Replies: 15
    Last Post: Jan 11th, 2006, 07:04 AM
  3. Replies: 5
    Last Post: Oct 13th, 2005, 06:17 AM
  4. Replies: 3
    Last Post: Sep 22nd, 2005, 10:14 AM
  5. Replies: 38
    Last Post: May 11th, 2005, 02:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •