• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Accessing session object from getConnection method

 
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
kundan varma
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jeanne i am talking about logged in user name which is in session
Thanks
kundan
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jeanne
kundan
 
kundan varma
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI Jeanne
It is possible to get user name in getconnection method using ThreadLOcal object.
Thanks
kundan
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kundan,
How would the ThreadLocal object get it? Regardless, I don't particularly like using threads in a J2EE application.
 
reply
    Bookmark Topic Watch Topic
  • New Topic