Link to home
Start Free TrialLog in
Avatar of dkim18
dkim18

asked on

weblogic - Setting data source in session

Hi,

I am trying to set data source in session, but getting null pointer exception. I checked data source and it was not null.
(ds: weblogic.jdbc.common.internal.RmiDataSource@63247b)

Here is the code:

Context initcntx = null;
               Hashtable ht = new Hashtable();  
               ht.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
               ht.put(Context.PROVIDER_URL, "t3://localhost:7001");  
             
               try {
                     
                     initcntx = new InitialContext(ht);
           
                     javax.sql.DataSource ds = (DataSource)initcntx.lookup(dsName);
                        session.setAttribute("ds", ds); ---------------> error!
...
...
...
++++++++++++++++=
java.lang.NullPointerException
        at RegistrationController.init()V(RegistrationController.java:60)
        at javax.servlet.GenericServlet.init(Ljavax/servlet/ServletConfig;)V(Gen
ericServlet.java:258)
        at weblogic.servlet.internal.ServletStubImpl$ServletInitAction.run()Ljav
a/lang/Object;(ServletStubImpl.java:1094)
        at weblogic.security.acl.internal.AuthenticatedSubject.doAs(Lweblogic/se
curity/subject/AbstractSubject;Ljava/security/PrivilegedAction;)Ljava/lang/Objec
t;(AuthenticatedSubject.java:321)
        at weblogic.security.service.SecurityManager.runAs(Lweblogic/security/ac
l/internal/AuthenticatedSubject;Lweblogic/security/acl/internal/AuthenticatedSub
ject;Ljava/security/PrivilegedAction;)Ljava/lang/Object;(SecurityManager.java:12
1)
        at weblogic.servlet.internal.ServletStubImpl.createServlet()Ljavax/servl
et/Servlet;(ServletStubImpl.java:970)
        at weblogic.servlet.internal.ServletStubImpl.createInstances()V(ServletS
tubImpl.java:949)
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

Why do you need to put the datasource in the session? It is not necessary. You can call the DataSource at anywhere on your codes. It is not *expensive* and all the connections are handled effectively by WebLogic.

However, if you would like to put it in session, I will help you ;). Can you post your current codes? How do you initialise the session?

David
Avatar of ss_p
ss_p

in case you have a request object in your scope you can set the object in the session as

request.getSession().setAttribute("ds", ds);

Thanks/SSP