• 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

possibility of getting user name from session inside 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(spc. kyle brown)
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
 
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 michael ernest
I am waiting for ur views.
Thanks
kundan
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you initialising "conn" to null, depriving yourself of a useful compiler check, and misleading the reader into thinking you might be using it without assigning to it first? Why are you returning null when you can't get a connection, where a (runtime) exception is almost certainly more appropriate? Are you really sure you want to clobber the connection's autocommit mode in a managed environment? Have you ever considered Log4J? Consider playing nice and actually close()ing the InitialContext once you're done with it, in a finally clause.

More to the point passing credentials and baggage like that around can be a pain. Consider using a ThreadLocal variable where you bind some kind of "call context" in your session bean facade. This mechanism works for us, and gives our code a tier-agnostic way of retrieving the caller principal and the like. Without any pollution of method signatures.

- Peter
 
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 Peter
Frankly speaking i couldnt understand your suggestions about this code.If i dont put null to Connection then how can i return it in case if it doesnt get connection.Also how to use THreadLocal and avoid parameter pollution.Can you provide some code snippet.
Even last time i learnt a lot from you for compareTo method.And i am sure this time also i will learn a lot of new things.One more thing yes i am using Log4J in this project.
THanks
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 Peter
AS far as getting username is concerned, i did it with ThreadLOcalObject. Thanks a lot for giving the clue.What about other suggestion which u provided??
One more thing, tell me how u r in touch with almost everything.Do follow some book or some site for continuos update.
THanks
kundan
 
Peter den Haan
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by kundan varma:
Frankly speaking i couldnt understand your suggestions about this code. If i dont put null to Connection then how can i return it in case if it doesnt get connection.

But you never do. You use the null literal in your return statements. You're never actually using that null value you assign to "conn", so it shouldn't be there at all.

I don't think it's a good idea to return null at all though - if you can't find your DataSource, or the DataSource cannot give you a database connection, there's no meaningful recovery you can perform. The best way to handle such disastrous errors is to wrap them into an unchecked exception (eg a DataAccessException extends RuntimeException), allow them to propagate up the call stack, and handle them at the top level, for example a web application error page (ie use web.xml to map DataAccessException to a page that says "Sorry, we have problems, try again later"). Assuming that such exceptions are logged, you don't need to log the error here.

What I missed first time round is that the code is not threadsafe; multiple threads may access the getConnection method and you may unnecessarily look up the DataSource multiple times. No disaster but not nice. Any access to "dataSource" should really be synchronized.

Given these points, the code could be amended to:I did get rid of the unused "from" parameter.

These days, I would not easily write plumbing like this; the Spring bean factory looks up DataSources for me, and (if working directly with JDBC) the JDBC template would do all the work with Connections and translating SQLExceptions to runtime exceptions. But that's another discussion

- Peter
 
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 Peter
One more thing i want to ask about ThreadLocal object.Does it have bad impact on performance.Whats your view on this???
Thanks for all your helps
kundan
 
Peter den Haan
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by kundan varma:
One more thing i want to ask about ThreadLocal object. Does it have bad impact on performance.

No. It is clearly slower than, say, an instance variable, but completely insignificant compared to the time it takes to access a JDBC database. No need to worry whatsoever. You might be interested to learn that ThreadLocal objects are widely used by application servers to keep track of the caller principal, transaction contexts and so on.

- Peter
 
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 Peter
One more ThreadLOcal question.Will this solution works if my web container and ejb containers are on different servers. Do they have the same threads or different threads .Does it adhere to j2ee specification.
Thanks
kundan
 
Peter den Haan
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, it works only if they are collocated (i.e. web tier and EJB tier on the same JVM), and I would hesitate to use ThreadLocal across tiers. Local EJBs might be alright, if there's no other way. For anything else use plain old method parameters.

- Peter
 
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
Hmmm then this will not work for me because they might be on different jvm
during deployment.
Any ways Thanks Peter
kundan
 
Peter den Haan
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I clearly don't quite understand your problem. Assuming you use container authentication, the caller principal is available from the EJBContext. You can, if you wish, bind it to a ThreadLocal object in your EJB or or pass it to the DAO explicitly. Where does the web tier come into the picture?

- Peter
 
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 Peter
Actually i am not using caller principal.INstead we have our own
user name password authentication.THis user name is entered by the user from browser and stored in servlet session.Then our action class which is on web tier connects the stateless session beans, these session beans in turn calls getconnection method.Is it possible if i store username in controller servlet and then again store that in new thread's threadLOcal
in webservice locator.So when the user tries to connect to session bean using webservice locator which will again span a new thread, at that time delegate my username stored in threadLOcal of web tier to threadlocal of ejb tier.
Whats your view on this??
THanks
kundan
 
Peter den Haan
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you're making life difficult for yourself by not using container authentication. If the EJB tier is not local, ThreadLocal won't help you. Your easiest choice at this stage, if hardly the most elegant, is probably to explicitly pass in the username as a method argument.

- Peter
 
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
Yes You are right.Actually i had no option as mine was a conversion project where database things and existing structures cannot be changed.I think i have to take all the pain and changeparameter everywhere.
Thanks Peter
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 Peter can you refer some good material for EJB and architect designing stuffs.
Thanks
kundan
 
Peter den Haan
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can I be perverse and recommend "J2EE without EJB" by Rod Johnson and Juergen Hoeller (declaration of prejudice: I was one of the book's reviewers)? Other essentials include the GoF Design Patterns book, Fowler's Patterns of Enterprise Application Architecture and Refactoring, Hunt ant Thomas's The Pragmatic Programmer, and Johnson's J2EE 1-on-1. Other than that, the Bunkhouse has plenty of good book reviews. More important than this, do your job, and engage with what you're doing; discuss it, quarrel about it, don't take anything for granted. True knowledge does not come out of a book. True knowledge comes out of you fighting with the material in practice and in discussion with others.

- Peter
[ August 30, 2004: Message edited by: Peter den Haan ]
 
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 Peter
"More important than this, do your job, and engage with what you're doing; discuss it, quarrel about it, don't take anything for granted. True knowledge does not come out of a book"

Yes you are right.Infact i do quarrel about every topic before implementing it
THANKS
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 Peter
Today only i changed my web server and application server on different machines.But still both are oc4j.Actually what i wanted to know is there any means that i can track or delegate variable stored in THreadLOcal of web server to the thread of appserver.I couldnt track when or exactly at what position ejb thread is getting created.I can see both the threads on diffrent machines.
Basically i want to track which webserver thread corresponds to which app server threads. That is one-one relation between different web tier threads and application tier threads.
WAiting for your reply
Thanks
kundan
 
Peter den Haan
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's no simple relation, the underlying mechanism (RMI) basically gives you no guarantees whatsoever on the number of threads or thread identity. ThreadLocal is useless here.

- Peter
 
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
I did one thing i send the thread name of web tier as parameter to ejb tier where i correspond to them with that parameter + Thread.currentThread.
Just to make a relationship between them.
Thanks
kundan
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic