-->
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: using more than one database user with connecion to database
PostPosted: Wed Nov 09, 2005 8:10 pm 
Newbie

Joined: Wed Nov 09, 2005 11:03 am
Posts: 5
Hello,

I have problem with using more than one database user with connecion to database.

I want to connect to my database using more then one user. Everywhere in examples and in doc there is use only one user to connect to database, for example defined in hibernate.properties (or in xml) file:
hibernate.connection.username = myuser

We have a SessionFactory object which we use to connect with database, it have determined user name and pass.

How connect to the same database(the same table) using difrent user and pass ?

I know how to connect to difrent database. I use more then one SessionFactory to other database, but in my example I have one database.

I cannot create more then one SessionFactory(something like HibernateUtil slass) becouse there is now possibility to dupicate classes maped in cfg files.

For more details:
I want to have two users(difrent login and pass in connecting to database layer). For exmaple first user can only get data from class: UserInfo, the secound user can get and insert data from the same class: UserInfo.
That is the reason i cannot create two .cfg.xml file. I cannot insert there maping for the same object:
<mapping resource="/auction/UserInfo.hbm.xml"/>
becouse it will be duplicated in whole application and error will ocure.


How realise taht multiuser-access in hibernate ?


Top
 Profile  
 
 Post subject: Multiple SessionFactories
PostPosted: Wed Nov 09, 2005 10:27 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You need one SessionFactory per user, to do this. You need to use different configurations for each factory: either provide your own connections, or at least don't fall back on the default configuration file. You can specify the configuration file when you create the SessionFactory, or you can use the hibernate Configuration object to configure the factories.

You need to use the same transaction manager for each one, or else you'll get into some serious problems with locking. As far as I know, all transaction managers support multiple connections to the same database: they'd be fairly basic managers to support only one.

Just be very, very careful about locks and terminating transactions if you want to use multiple connections. The more usual solution to this is to just have one connection with read and write access, and implement security in your session or business logic.


Top
 Profile  
 
 Post subject: article
PostPosted: Thu Nov 10, 2005 2:50 am 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
This is how you can do it:
http://sandbox.sourcelabs.com/kosta/hib ... ation.html

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 10, 2005 3:36 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Connection connection = connect(url,user,psw);
Session s = factory.openSession(connection);


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 12, 2005 6:43 pm 
Newbie

Joined: Wed Nov 09, 2005 11:03 am
Posts: 5
Thanks very much,

I created HibernateUtil with static block:

Code:
static {
        try {
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            log.error("Initial SessionFactory creation failed.", ex);
            throw new ExceptionInInitializerError(ex);
        }
}


and I have a method:

Code:
public static Session getSession( String instance ) throws HibernateException, SQLException {
        Connection connection = DriverManager.getConnection( url, instance, psw );
        Session session = sessionFactory.openSession(connection);
        return session;
}


I have url and psw initialized from beans.
And it works :)

Thanks very much again.


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.