• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Changing Hibernate configuration properties programmatically

 
Roberto Pasciuti
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody, I've problems trying to set Hibernate configuration programmatically (I'm using Hibernate 3) and I hope you have an idea of what I'm doing wrong.

I need to write some data to a DB. I get the connection data to DB from a WebService so I need to configure Hibernate at run time. What I need to set are: connection url, userName, password. Now, here is how I'm trying to do (with some simplification to better explain).



When I need, I call a method to do this (configuration and sessionFactory are the same above):



When trying to begin transaction, I obtain this error:

org.hibernate.exception.GenericJDBCException: Cannot open connection
.
.
Caused by: java.sql.SQLException: Connections could not be acquired from the underlying database!
.
.
Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.

If I configure hibernate.cfg.xml adding the missing properties, it works. So there must be something wrong in the way I'm trying to set/change the properties and/or to get the sessionFactory.
I'm looking for a solution on the web but I'm not successfull yet.

Have you any idea?

 
Vijitha Kumara
Bartender
Pie
Posts: 3930
21
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have tried setting all the required properties dynamically rather than the 3 properties shown?

And welcome to the CodeRacnh!
 
Roberto Pasciuti
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vijitha, thanks for your welcome.

I've tried your suggestion, so my code now is:


and here's the result:



And I've no idea...
 
Manuel Petermann
Ranch Hand
Posts: 177
Hibernate Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which line of code is causing this exception?
Hardcoding all of those things in Java isn't a good idea imho.
 
Roberto Pasciuti
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got the solution!!!

The problem was the name of the properties. When setting them with configuration.setProperty(...) method, the name to use is different than the name you use in hibernate.cfg.xml. So, in code:


Now, what I'd like to know is if it existes, and where it is, the list of all the correct property names. I mean, have I simply to add "hibernate" prefix to every property? For example, I would write "hibernate.c3p0.max_size" but somewhere I've found "hibernate.c3p0.maxSize". There's a rule? Depends on Hibernate version? Or what?

Oh well, if I find it, I'll post it.
 
Roberto Pasciuti
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Manuel, I agree you: hardcoding is never a good idea, but I was simply testing.

Now I'm back to my fisrt solution, that is to set only the 3 params I need using setProperty() method.

Thanks for your willingness.
 
Vijitha Kumara
Bartender
Pie
Posts: 3930
21
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roberto Pasciuti wrote:...I would write "hibernate.c3p0.max_size" but somewhere I've found "hibernate.c3p0.maxSize". There's a rule? Depends on Hibernate version? Or what?
...

Find Hibernate specific properties for c3p0 here.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic