-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Datasource dynamically according to a session variable
PostPosted: Tue Nov 17, 2009 5:45 am 
Newbie

Joined: Tue Nov 17, 2009 4:53 am
Posts: 7
Hi all,

I want my application to dynamically based on user login to change the datasource hibernate.cfg.xml

I have a session variable that I get when the user logs in:
#{sessionScope.utilisateur.datasource}

I then replace the XXX line:
<property name="connection.datasource">java:comp/env/XXX</property>
by this variable.

My hibernate.cfg.xml functional without datasource dynamic :

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
      "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
      "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
         <property name="connection.datasource">java:comp/env/XXX</property>
      <property name="show_sql">true</property>
      <
    </session-factory>
</hibernate-configuration>


I tried to change the configuration hibernate through this code in the HibernateSessionFactory.java:


Code:
Configuration configuration = new Configuration()
.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect")
.setProperty("hibernate.connection.driver_class", "org.postgresql.Driver")
.setProperty("connection.datasource", "java:comp/env/"+"XXX")
.setProperty("show_sql", "true")


SessionFactory factory = configuration.buildSessionFactory()



However I have always this error:

Code:
Caused by: java.lang.UnsupportedOperationException: The user must supply a JDBC connection
   at org.hibernate.connection.UserSuppliedConnectionProvider.getConnection(UserSuppliedConnectionProvider.java:30)


Someone would have an idea?

In advance thank you!

P.S: Sorry for my bad English...


Top
 Profile  
 
 Post subject: Re: Datasource dynamically according to a session variable
PostPosted: Tue Nov 17, 2009 9:18 am 
Newbie

Joined: Tue Nov 17, 2009 4:53 am
Posts: 7
finally the solution :

configuration.setProperty("hibernate.connection.datasource", "xxxx");

in hibernatesessionfactory


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.