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.  [ 9 posts ] 
Author Message
 Post subject: Why buildSettings does NOT throw HibernateException?
PostPosted: Tue Oct 18, 2005 4:59 am 
Newbie

Joined: Fri Feb 04, 2005 9:45 am
Posts: 8
Location: Czech Republic
Hibernate version:3.0.5

Why buildSettings from SessionFactory doesn't throw HibernateException any more? In version 2.1.1 it does but in version 3 it doesn't.

Was it purpose or just mistake?

Any way if it was purpose can you give me some advice how to chceck if user can connect to DB ? (username and password are NOT written in hibernate properties, but are taken dynamically as user input)

I thought that if user will provide unsufficient username and password the SessionFactory would throw HibernateException and so user has no rights to access this database.

What would you suggest to solve this problem?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 18, 2005 5:05 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
the stuff done in buildSettingsFActory could fail for other reasons than user/password errors which could just be because of lack of credentials which wont affect the remainder of the application.

just try and execute a query or something and you will know...

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 18, 2005 5:21 am 
Newbie

Joined: Fri Feb 04, 2005 9:45 am
Posts: 8
Location: Czech Republic
I know , but actually I need to have
List allDatabases
and
List databasesForUser


so when I am trying to buildSettings I want to (if I got SessionFactory ) to add this to the databasesForUser


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 18, 2005 5:34 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
i don't understand.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 18, 2005 5:52 am 
Newbie

Joined: Fri Feb 04, 2005 9:45 am
Posts: 8
Location: Czech Republic
ok ,
i have application which is trying to get session upon (lets say )5 different DB. (let's say you want to log to DB- Paris, DB-London, DB-Prague, DB-Berlin, DB-Zurich, but the DB structure is same)

Some users have only acces to some of them.

thats why I use
List allDatabases (it has objects of HibernateDatabase for all databases which are in configuration file)

and

List databaseForUser(which has object HibernateDatabase only for that database user provide sufficient username/password)

public HibernateDatabase() {

private String databaseName;

private String dialect;

private String conectionURL;

private String conectionDriver;

private String cacheProvider;

.....

}

When user will try to log he will iterate over the collection with all databases and try to log there with provided user's username/password.
If he gets SessionFactory(if it buildSettings) it means that he has rights to access database (and that DB will be added to users List) and if he gets Exception or null it would mean that user has no rights to that database (and that DB will NOT be added to the User's list).


AnyWay when I look at the hibernate SettingsFactory it has change. Why it doesn't throw HibernateException? There is only try/catch with warning but id doesn't throw exception.

version 2.1
Code:
public final class SettingsFactory {
   
   private static final Log log = LogFactory.getLog(SettingsFactory.class);
   
   public static Settings buildSettings(Properties properties) throws HibernateException {



version 3.0.5
Code:
public class SettingsFactory implements Serializable {
   
   private static final Log log = LogFactory.getLog(SettingsFactory.class);

   protected SettingsFactory() throws HibernateException {}
   
   public Settings buildSettings(Properties props) {


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 18, 2005 7:23 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
and why doesnt you just try and open a connection to it via the session ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 18, 2005 8:39 am 
Newbie

Joined: Fri Feb 04, 2005 9:45 am
Posts: 8
Location: Czech Republic
I have solved it by running direct sql and try to connect. If it fails it throws exception.


Code:
Connection con = DriverManager.getConnection(url, userName, password);


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 18, 2005 8:54 am 
Newbie

Joined: Fri Feb 04, 2005 9:45 am
Posts: 8
Location: Czech Republic
If I understood you correctly your suggestion was like that:

Code:
SessionFactory factory =  cfg.buildSessionFactory();
Session sess = factory.openSession();


I will be able to create factory (it warns me that there is SQLException) but it goes ahead.
I am able even to create Session from factory.
It throws first exception after I try to retrieve some data from database.

So I have solved it with this sollution:

Code:
try{
SessionFactory factory =  cfg.buildSessionFactory();
Connection con = DriverManager.getConnection(properties.getProperty(HibernateConstants.HIBERNATE_CONFIG_URL),properties.getProperty(HibernateConstants.HIBERNATE_CONFIG_USERNAME),properties.getProperty(HibernateConstants.HIBERNATE_CONFIG_PASSWORD));
con.close();
}catch{...


If I give wrong username/password factory (with warning) runs fine but Connection throw exception.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 18, 2005 9:01 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
or get the ConnectionProvider from ((SessionFactoryImpl)sf).getConnectionProvider() and try and open a connection from there (remember to release it back) ...that will work even without a DriverMAnager

_________________
Max
Don't forget to rate


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