Want my application to see if caching disabled then skip database connection
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Vijendra_Singh
Posted On:   Thursday, December 11, 2008 02:58 AM

I have a situation here. Want to bypass database connection if caching disabled. If caching found enable, only then try to establish a database connection. One part of application should work when database is down as it doesn't require connection to database at all. While other parts should throw exception when database connection not available. the piece of code looks like this: public byte[] getStagedSchedule(long begin, int channelID, String stage, String username, String password, Number releaseID, Number planningTypeId ) throws SchedExExportException, SecurityServiceException { byte[] ret = null; try { Date myDate = new Date(begin); this.stageCo   More>>

I have a situation here.
Want to bypass database connection if caching disabled. If caching found enable, only then try to establish a database connection.
One part of application should work when database is down as it doesn't require connection to database at all.
While other parts should throw exception when database connection not available.
the piece of code looks like this:


			

public byte[] getStagedSchedule(long begin, int channelID, String stage, String username, String password, Number releaseID, Number planningTypeId ) throws SchedExExportException, SecurityServiceException
{
byte[] ret = null;
try
{
Date myDate = new Date(begin);
this.stageConfig = new StageConfig(stage, new Integer(channelID), myDate);
if(!this.stageConfig.isCached())
{
IService service = null; //database connection not required here (can handle that part)
StageAccessService myStage = new StageAccessService(service, myDate, new Integer(channelID), stage,username, releaseID, planningTypeId);
try{
ret = myStage.getXML();
}
catch(SchedExExportException e){
LogUtils.getExtLogger().error("PSIStopProcessException: " + e.getMessage(), null, null, e);
throw new SchedExExportException(e);
}
}
else{
IService service = ServiceFactory.getInstance("TxSchedEx"); //database connection string
StageAccessService myStage = new StageAccessService(service, myDate, new Integer(channelID), stage,username, releaseID, planningTypeId);
ret = myStage.getXML();
if(service != null)
{
service.release();
}
}
}

catch (ServiceException e)
{
LogUtils.getExtLogger().error("ServiceException: " + e.getMessage(), null, null, e);
throw new SchedExExportException(e);
}

return ret;
}




Now this code is working as per my requirement. for the time being.
But have two major problems to be solved here.

1) this piece of code :
this.stageConfig = new StageConfig(stage, new Integer(channelID), myDate);
is getting called in other class as well. i.e. inside this:
StageAccessService myStage = new StageAccessService(service, myDate, new Integer(channelID), stage,username, releaseID, planningTypeId);
have initialise method. which internally calls this method:



			

private boolean initialize(IService service, Date date, Number channel, String stage, Number releaseId, Number planningTypeId) throws PSIStopProcessException
{
boolean ret = false;
if ( ( null != service ) && ( null != date ) && ( null != channel)&& ( null != stage))
{
this.service = service;
this.scheduleDate = date;
this.scheduleChannel = channel;
this.stageConfig = new StageConfig(stage, channel, date);
this.planningTypeId = planningTypeId;
this.givenReleaseId = releaseId;
ret = true;
initRelease(releaseId);
}
else if ( ( null == service ) && ( null != date ) && ( null != channel)&& ( null != stage)) // just to handle if database connection not available.
{
this.scheduleDate = date;
this.scheduleChannel = channel;
this.stageConfig = new StageConfig(stage, channel, date);
this.planningTypeId = planningTypeId;
this.givenReleaseId = releaseId;
ret = true;
}
return ret;
}





Because StageAccessService is the real entry point for all other classes to access information about stage (staging). (So making StageConfig singleton, won't help me)

Now is there any way i can avoid :
this.stageConfig = new StageConfig(stage, channel, date);
getting called twice in my code.

why this is required is this method checks for caching real values.



			

public byte[] getXML() throws SchedExExportException
{
byte[] ret = null;
if ( !this.stageConfig.isCached() )
{
ret = dynamicLoadXML();
}
else
{
ret = loadReleaseFromStage();
if (ret == null && this.stageConfig.isLazyLoading())
{
ret = dynamicLoadXML();
saveScheduleInStage(ret);
}
}
and so on.............
try{
if (this.stageConfig.isTransformSchedules() && ret != null)
{
etc





2) Other workaround can be like: check if database connection is there or not. log the exception in catch block. but don't throw back.
So that the part which doesn't require database connection can work. But if request is not calling that part, then better throw exception also.

I don't know whihc approcah is good in this case.

Can anyone suggest anything better then this.

Thanks
vijendra    <<Less
About | Sitemap | Contact