-->
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.  [ 5 posts ] 
Author Message
 Post subject: Setting JDBC url and credentials dynamically at runtime
PostPosted: Wed Sep 03, 2008 1:02 pm 
Newbie

Joined: Wed Sep 03, 2008 12:46 pm
Posts: 6
Hibernate version: 3.2

Name and version of the database you are using: DB2 8

Pardon my newbieness! I'm working with 3.2 Core (no annotations), using mappings, DOA's, and a session factory automatically generated by MyEclipse 6.5.

I'm trying to build a internal web app for maintaining some small sets of data. The kink is that this admin tool can be pointed at one of multiple datasources (I'd like to use a pull-down list on the login page)... and we'd like to connect to the datasource with the credentials supplied on the login page.

What I have tried to do is modify the session factory class generated by MyEclipse... to include static member variables for the JDBC connection url, the username, and the password. Then, I modified the method which instantiates the session factory, to look like this:

configuration.configure(configFile); // "/hibernate.cfg.xml"
configuration.setProperty("connection.url", jdbcURL);
configuration.setProperty("connection.username", jdbcUsername);
configuration.setProperty("connection.password", jdbcPassword);
sessionFactory = configuration.buildSessionFactory();

Unfortunately, from playing around in the debugger, it looks like this first line (which was there originally) causes Hibernate to go out and hit the database to setup the various mappings. Since the URL, username, and password haven't been set yet, it bombs at that point. So I'm in a bit of a Catch-22, in that I cannot add my custom properties to the Configuration... because they need to be there when instantiating the Configuration.

Is there something obvious I'm missing, or a better approach I should be using? I imagine that it wouldn't be that uncommon to dynamically set JDBC url's or credentials at runtime. Thanks in advance!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 03, 2008 2:18 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
We are using a similar approach in our project. We are calling the methods in another order, and doesn't specify a file in the call to configure().

Code:
configuration.setProperty("connection.url", jdbcURL);
configuration.setProperty("connection.username", jdbcUsername);
configuration.setProperty("connection.password", jdbcPassword);
configuration.configure();
sessionFactory = configuration.buildSessionFactory();


Top
 Profile  
 
 Post subject: Re:
PostPosted: Wed Sep 03, 2008 2:25 pm 
Newbie

Joined: Wed Sep 03, 2008 12:46 pm
Posts: 6
Ahh... that did the trick. Thanks!


Top
 Profile  
 
 Post subject: Re: Setting JDBC url and credentials dynamically at runtime
PostPosted: Wed Jun 17, 2009 9:59 pm 
Beginner
Beginner

Joined: Thu Jul 22, 2004 10:51 pm
Posts: 29
Location: sydney australia
i'm trying to do the same thing, but can't get it to work as you've described.

i've commented out the connection values in my hibernate.cfg.xml and then run with the following:

Code:
Configuration config = new Configuration();
config.setProperty("connection.url", init.getDatabaseUrl());
config.setProperty("connection.username", init.getDatabaseUser());
config.setProperty("connection.password", init.getDatabasePass());
config.configure();
sessionFactory = config.buildSessionFactory();

when this is run i get the following warning, meaning the setProperty calls have had no effect
Code:
WARN  org.hibernate.connection.UserSuppliedConnectionProvider - No connection properties specified - the user must supply JDBC connections

then when the connection is tested, it fails.
Code:
java.lang.UnsupportedOperationException: The user must supply a JDBC connection
   at org.hibernate.connection.UserSuppliedConnectionProvider.getConnection(UserSuppliedConnectionProvider.java:54)
   at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
   at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
   at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:142)


any ideas?


Top
 Profile  
 
 Post subject: Re: Setting JDBC url and credentials dynamically at runtime
PostPosted: Wed Jun 17, 2009 10:16 pm 
Beginner
Beginner

Joined: Thu Jul 22, 2004 10:51 pm
Posts: 29
Location: sydney australia
sorry, solved it - we had the same error in all of our examples = the properties need to be prefixed with 'hibernate.'

so

Code:
Configuration config = new Configuration();
config.setProperty("hibernate.connection.url", init.getDatabaseUrl());
config.setProperty("hibernate.connection.username", init.getDatabaseUser());
config.setProperty("hibernate.connection.password", init.getDatabasePass());
config.configure();
sessionFactory = config.buildSessionFactory();


i'm sure i've run into this before!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.