This document was generated from CDN thread
Created by: Shahzad Munir on 12-09-2008 11:09:07 AM
I am developing an application comprising Action Elements for CVP 4.1. I need to communicate with the database and have to use Apache DBCP pool. I am initializing this pool (a static BasicDataSource object) in the Starup class which implements StartApplicationInterface. My Action class and Startup classes are bundled in the same jar and this jar along with dbcp related jars is placed in the application\java\lib folder.
Now Pool is initialized successfully in Startup but I find this pool un-initialized in my CustomAction class, thus, I am unable to get connection from BasicDataSource initialized in Startup.
One thing, DataSource reference is not in Startup class but its static member of another class.
I have done a work around for it and re-initialized pool first time my custom action is called. But I really didnt feel good bout this.
Under Design Considerations heading, its mentioned that static variables are static within the application only. Both Startup and CustomAction classes are bundled in the same jar, helping jars have been placed in the same dir, I am programming within the same application, and this makes me sure that I shouldnt be facing this problem.
Can any body please help me on this ??? It's urgent. How & where should I initialize my Apache DBCP Pool.
Thanking in anticipation
Subject: Re: Problem with Static Vars within Same Application
Replied by: Elam Birnbaum on 26-01-2009 08:24:03 PM
I am sorry to hear that there wasn't a response made to this question. It may be too late but I will take a crack at it.
You don't mention whether the class that contains the static variable with the DataSource you are trying to reference is also bundled in the JAR file yu deploy in the application's java folder. If it is not, then that could explain why you are seeing problems. If you bundle all the classes together you should likely not have problems.
Just as a note you would have to initialize your static variable inline or in a static block in order for it to have a non-null value. If you don't then your other classes will be able to see the variable but it will be null the first time you attempt to use it. So in fact your workaround is correct programming, always checking for null and re-initializing if it is null is good practice because you never know where it is going to be called first.
As mentioned in the documentation, static variables should be avoided. You are already using the application start class and any application data you declare there have essentially the same behavior as static variables, except managed better.
Hope this helps, and sorry again for the late response.