File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes Accessing session object from getConnection method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Accessing session object from getConnection method" Watch "Accessing session object from getConnection method" New topic
Author

Accessing session object from getConnection method

kundan varma
Ranch Hand

Joined: Mar 08, 2004
Posts: 322
HI All
I want to get username from session inside getConnection method without sending that as parameter.
My connection method is defined like this
public static Connection getConnection(String from)
{
Connection conn = null;
if(dataSource == null)
{
try
{
InitialContext ic= new InitialContext();
dataSource= (DataSource) ic.lookup("jdbc/OracleDS");
}
catch (NamingException e)
{
e.printStackTrace();
return null;
}
}

try
{
conn= dataSource.getConnection();
conn.setAutoCommit(false);
return conn;
}
catch(SQLException e)
{
e.printStackTrace();
return null;
}
}

I want to get a username inside this from session.THis method is getting called from stateless session beans.
I dont want to change the parameter because i have to reflect that at all places.Iwant to store username somewhere from where i acn fetch it inside this method .Is there any solution for this.
THanks
kundan


SCJP1.4,SCBCD,SCEA,CNA
Failures are practice shoots for success.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31062
    
232

Kudan,
Are we talking about the username to access the database or the name of the logged in user?

The username for the datbase can be read from a property file. The logged in user has to be passed to the method.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
kundan varma
Ranch Hand

Joined: Mar 08, 2004
Posts: 322
Hi Jeanne i am talking about logged in user name which is in session
Thanks
kundan
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31062
    
232

Kundan,
Then you have to pass your getConnection method either the session or a string username. I would prefer the later as it makes your code more flexible.
kundan varma
Ranch Hand

Joined: Mar 08, 2004
Posts: 322
Thanks Jeanne
kundan
kundan varma
Ranch Hand

Joined: Mar 08, 2004
Posts: 322
HI Jeanne
It is possible to get user name in getconnection method using ThreadLOcal object.
Thanks
kundan
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31062
    
232

Kundan,
How would the ThreadLocal object get it? Regardless, I don't particularly like using threads in a J2EE application.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing session object from getConnection method