-->
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.  [ 8 posts ] 
Author Message
 Post subject: Get a session with only a Connection object
PostPosted: Tue Jan 18, 2011 12:55 pm 
Newbie

Joined: Tue Jan 18, 2011 12:50 pm
Posts: 5
Hi everyone,
I'm trying to get a session without putting in hibernate.cfg.xml the connection parameters, because i don't have them.
The only thing i have accesible is a Connection object, that i get in any moment, but i can't extract the params in order to use Configuration.setProperty().

The problem is that a session (i think) can only be retrieved using a SessionFactory, and a SessionFactory can only be created using a Configuration. At the moment i request to the Configuration to buildSessionFactory(), it goes to the configuration xml and throws an error.

What i want, is to use "session = sessionFactory.openSession(MyConnectionObject);

Anyone knows by a chance how could i get it working??? I't would be very usefull.

Thanx in advance, and sorry about my english.

Regards, Andres


Top
 Profile  
 
 Post subject: Re: Get a session with only a Connection object
PostPosted: Tue Jan 18, 2011 7:26 pm 
Newbie

Joined: Tue Jan 18, 2011 7:11 pm
Posts: 5
You can implement org.hibernate.connection.ConnectionProvider and set the value of hibernate.connection.provider_class in your hibernate.cfg.xml file, or set it in code on the Configuration object if you need to do some initialization. Google "hibernate ConnectionProvider" and you'll find some examples.


Top
 Profile  
 
 Post subject: Re: Get a session with only a Connection object
PostPosted: Tue Jan 18, 2011 7:40 pm 
Newbie

Joined: Tue Jan 18, 2011 12:50 pm
Posts: 5
Thanx! I'll try tomorrow at work! Now it's 24:30 here,but i promise feedback and some code!

Regards!!!


Top
 Profile  
 
 Post subject: Re: Get a session with only a Connection object
PostPosted: Wed Jan 19, 2011 7:51 am 
Newbie

Joined: Tue Jan 18, 2011 12:50 pm
Posts: 5
I'm trying now, and the option i need is setting it in the code.
I made a CustomConnectionProvider, and in case i put the class in the hibernate.cf.xml in hibernate.connection.provider_class property, it throws that can't instantiate the class. And in the other side, if I remove all the properties in the xml, it crashes because it is asking for them....

I don't know how i could tell hibernate to ignore the xml....

my main() is only a system.out.println!!! i don't know why is trying to initialize all the BD stuff.


Top
 Profile  
 
 Post subject: Re: Get a session with only a Connection object
PostPosted: Wed Jan 19, 2011 8:36 pm 
Newbie

Joined: Tue Jan 18, 2011 7:11 pm
Posts: 5
I don't know what you mean by BD stuff, but hibernate expects your connection provider to have a no-args constructor. Did you look at this example?
http://www.javalobby.org/java/forums/t18406.html


Top
 Profile  
 
 Post subject: Re: Get a session with only a Connection object
PostPosted: Thu Jan 20, 2011 8:15 am 
Newbie

Joined: Tue Jan 18, 2011 12:50 pm
Posts: 5
Sorry, i mean DB stuff, not BD (in spanish Base de Datos, in english DataBase).

In that example, he pass several DB arguments:

Quote:
hibernate.connection.driver_class = org.postgresql.Driver
hibernate.connection.url = jdbc: postgresql://localhost/mydatabase
hibernate.connection.username = myuser
hibernate.connection.password = secret
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=50


And my big problem is that i don't know any of them before the execution. The connection is given from other source, and i don't have either url or user/password. The only thing i have is a Connection object.

So, my main procedure, has in the first line:

Quote:
hibernate.connection.driver_class = org.postgresql.Driver
hibernate.connection.url = jdbc: postgresql://localhost/mydatabase
hibernate.connection.username = myuser
hibernate.connection.password = secret
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=50


and, in order to doing tests, i have commented the rest of the actions, but hibernate is still trying to connect to the database:

Quote:
20-ene-2011 13:17:01 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
GRAVE: could not complete schema update
java.lang.UnsupportedOperationException: The user must supply a JDBC connection
at org.hibernate.connection.UserSuppliedConnectionProvider.getConnection(UserSuppliedConnectionProvider.java:30)
at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:27)
at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:127)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:296)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1176)
at com.nerviaconsultores.hibernate.ConsultaPrueba.main(ConsultaPrueba.java:34)
oracle.jdbc.driver.T4CConnection


What i really need, is that the hibernate initialization actions, don't get executed until i tell him... it's that posible?

Thanx again, and sorry about all of this...

Regards


Top
 Profile  
 
 Post subject: Re: Get a session with only a Connection object
PostPosted: Sat Jan 22, 2011 10:38 am 
Newbie

Joined: Tue Jan 18, 2011 7:11 pm
Posts: 5
You need to read the whole article. Just a few lines after what you quoted he writes

Maybe neither of those [jndi or connection pool] are options [that] will work for you.

And he then goes on to describe how to provide connections to hibernate in a legacy environment.


Top
 Profile  
 
 Post subject: Re: Get a session with only a Connection object
PostPosted: Mon Jan 24, 2011 4:18 am 
Newbie

Joined: Tue Jan 18, 2011 12:50 pm
Posts: 5
Sorry, buy i finally realised what was happening. I had the code correct, but at the execution time eclipse was not updating the binaries, so i was executing always the old version....

Thanx anyway, it's working!!!


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