views:

96

answers:

2

I have a simple task to accomplish, but I am not sure what is the best way to go by.

Each user has their own username and password to connect to a database with different privilege. Once the user connect, he will do multiple query base on what action he want to perform. Therefore I want to retain the connection with the database. So here is my question?

Is it better to achieve what I want to do with connection pooling or session or both? Sample code would be appreciated!!! thank you very much.

+1  A: 

Define a container managed connection pooled DataSource with "root" privileges in the server and for each user make use of the DataSource#getConnection() method which takes an username and password as arguments.

BalusC
I am reading on it from your tutorial. Will ask question If I stuck on anything... thank you very much
Harry Pham
quick question: In your tutorial about how to use JNDI Datasource(http://balusc.blogspot.com/2008/07/dao-tutorial-data-layer.html#PropertiesFileLoader), you have: ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); InputStream propertiesFile = classLoader.getResourceAsStream(PROPERTIES_FILE); I cant get that to work. Keeping getting propertiesFile == null. I have to properties file where my project is, not where my .java or .class are. I changed the two above lines to below in order to make it work: propertiesFile = new FileInputStream(PROPERTIES_FILE); Any idea why?
Harry Pham
Put it in one of the root paths which is by default already covered by the classpath (e.g. project's `src` folder, or `/WEB-INF/lib`, or `/WEB-INF/classes`), or add the file to the project's *Build Path* (which basically just represents both compiletime and runtime classpath), or -better- add its root path at local disk file system to the classpath of the runtime environment --in for example Tomcat you can specify it in the `shared.loader` property of `/conf/catalina.properties`.
BalusC
thank you. It worked :)
Harry Pham
You're welcome.
BalusC
I hate to bother u BalusC, but I got some weird error. :(when I create a connection, if I use dataSource: return dataSource.getConnection(username, password); I got this error. SQLException: Cannot create PoolableConnectionFactory (Access denied for user ''@'152.17.133.126' (using password: NO)). Clearly that I am passing in username and password, but the error show that I did not have username and password. If I change that line from dataSource.getConnection() to DriverManager.getConnection(url), then it worked. 152.17.133.126 is not even the address of the database.
Harry Pham
Are the username/password of "root" user definied in datasource?
BalusC
it is not. Should I? since I expected to have multiple client log in, can I defined list of username and password in the datasource?
Harry Pham
I've exactly answered this in my original answer, reread it: *Define a container managed connection pooled `DataSource` with "root" privileges in the server and for each user make use of the `DataSource#getConnection()` method which takes an username and password as arguments.*
BalusC
I now have username and password of "root" defined in the datasource. I also create different user with permission to connect to database.DataSource#getConnection(username, password) does not give me back a connection, however if I use DataSource#getConnection() then it work. Seems like I can only connect to the database with one user. When u said " Define a container managed connection pooled DataSource with "root" privileges in the server", do u just mean, create a user that has root privilege in database, and defined them in the datasource, is that right?
Harry Pham
The "root" user ought to be the owner of the other users. I however just start to realize that this may be DB and/or JDBC driver dependent. Which ones are you using?
BalusC
Database: mysqlJDBC driver: com.mysql.jdbc.DriverWell I log in the DB as "root" and create other users, does it make me the owner of other user?
Harry Pham
This tutorial that you created on `DAO tutorial - the data layer`, you mention that about implement connection pool in Apache 6. If I am using glassfish v3. Does the implementation still apply the same way?
Harry Pham
Yes. The only difference is the way of creating the JNDI datasource. Glassfish doesn't understand `/META-INF/context.xml` like Tomcat. Follow this link for examples and more information: http://www.google.com/search?q=glassfish+create+jndi+datasource
BalusC
It seems like we dont need to create properties file for glassfish connection pool. The interface took care of it. Thank you a lot
Harry Pham
A: 

I faced the same issue with an Oracle DB, where every transaction has to be record for possible audits. So, every time a user authenticates in the web application, it puts an object like this:

public class ConnectionUser implements Serializable {

private String userId;
private String password;
private String transactionKey;
//setters and getters

public boolean equals(Object object) {
    if(!(object instanceof org.ampf.af.jdbc.connection.ConnectionUser)) {
        return false;
    }
    if(object == null) {
        return false;
    }
    return (this.getUserId().equals(((ConnectionUser)object).getUserId()) && 
            this.getPassword().equals(((ConnectionUser)object).getPassword()));
}

Then I implemented a connection pool (based on a Map) where the key is the ConnectionUser object and the value is the open connection. If the key doesn't exist in the map, then it creates a connection and allocates it into the map.

I hope it helps you.

Aito