JavaScript Caching

Many browsers cache the ajax calls. This is annoying sometimes. There might be many techniques to overcome this browser caching, here is what I found on


This is simple and easy. As you you might notice the reason why this works is, the request is always different from all the previous one since the timestamp is part of the request.

nice job...

The other ways to restrict HTML caching is on



SQL SERVER 2000 with SP4

With SQL 2000 server using com.microsoft.jdbc.sqlserver.SQLServerDriver as the SQL driver class from msbase.jar, mssqlserver.jar and msutil.jar if you hit a error like

SEVERE: Standard SQL Exception Info for exception at level 0 - SQL State: 'HY000'; SQL Error Number: '0'; SQL Error Text: '[Microsoft][SQLServer 2000 Driver for JDBC]The DBMS returned an unspecified error.';[Microsoft][SQLServer 2000 Driver for JDBC]The DBMS returned an unspecified error. at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source) at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source) at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReplyToken(Unknown Source) at com.microsoft.jdbc.sqlserver.tds.TDSRPCRequest.processReplyToken(Unknown Source) at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown Source) at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.getNextResultType(Unknown Source) at com.microsoft.jdbc.base.BaseStatement.commonTransitionToState(Unknown Source) at com.microsoft.jdbc.base.BaseStatement.postImplExecute(Unknown Source) at com.microsoft.jdbc.base.BasePreparedStatement.postImplExecute(Unknown Source) at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source) at com.microsoft.jdbc.base.BaseStatement.executeUpdateInternal(Unknown Source) at com.microsoft.jdbc.base.BasePreparedStatement.executeUpdate(Unknown Source) at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:102)

Then the issue is with the jdbc driver, upgrade your driver to SQL 2005 server driver. This will resolve the problem. The jar will be sqljdbc.jar. If you are planning to use the latest jar of about 570KB this is good but you might hit the other error.


SEVERE: Standard SQL Exception Info for exception at level 0 - SQL State: '08S01'; SQL Error Number: '0'; SQL Error Text: 'The TDS protocol stream is not valid.';SEVERE: com.microsoft.sqlserver.jdbc.SQLServerException: The TDS protocol stream is not valid. at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(Unknown Source) at com.microsoft.sqlserver.jdbc.SQLServerConnection.throwInvalidTDS(Unknown Source) at com.microsoft.sqlserver.jdbc.TDSReader.throwInvalidTDS(Unknown Source) at com.microsoft.sqlserver.jdbc.TDSParser.throwUnexpectedTokenException(Unknown Source) at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onRetValue(Unknown Source) at com.microsoft.sqlserver.jdbc.TDSParser.parse(Unknown Source) at com.microsoft.sqlserver.jdbc.SQLServerResultSet$FetchBuffer.nextRow(Unknown Source) at com.microsoft.sqlserver.jdbc.SQLServerResultSet.fetchBufferNext(Unknown Source) at com.microsoft.sqlserver.jdbc.SQLServerResultSet.next(Unknown Source) at org.apache.tomcat.dbcp.dbcp.DelegatingResultSet.next(DelegatingResultSet.java:169)


To resolve this download and use the Microsoft SQL Server 2005 JDBC Driver 1.2 or 1.1 from
http://msdn.microsoft.com/data/jdbc

SQL SERVER database and user







1) First create the user in the sercurity/Logins by right clicking in the right window.



2) Enter the login name and the password. If you are allowing the external users to connect then select the sql server authentication. Also select the database you allow the user to access.

3) Select the appropriate roles for the user

4) Finally in the Database Access page select the databases you allow the user to access. Click Ok and save the user and the credentials. Do not confuse with the one selected in the section 2. That is the default database, the ones you select now are the databases the user is permited.


5) Now open the database you want the user just created to access. Select the user and in the right window right click and select New Database User.
6) Enter the user name and the password and you are all set. The above can be done by selecting the in the user name.

Now when you try to write a java application to connect to this database, you may see the error/exception as
Exception in thread "main" java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)at com.microsoft.jdbc.sqlserver.tds.TDSConnection.(Unknown Source)at com.microsoft.jdbc.sqlserver.SQLServerImplConnection.open(Unknown Source)at com.microsoft.jdbc.base.BaseConnection.getNewImplConnection(Unknown Source)at com.microsoft.jdbc.base.BaseConnection.open(Unknown Source)at com.microsoft.jdbc.base.BaseDriver.connect(Unknown Source)at java.sql.DriverManager.getConnection(DriverManager.java:512)at java.sql.DriverManager.getConnection(DriverManager.java:193)






This can be solved by following the steps below.

First try the telnet 1433, if this fails then the server configuration has to be changed.

1. Make sure that SQL Server is set to mixed authentication. To do this, open enterprise manager, right click on server registered on there, go to security tab and select SQL Server and Windows Authentication.

2. Go to the general tab and click the network configuration button. Enable TCP/IP. Click properties to change the default port (1433).

3. Add an SQL Server login account (NOT a WINDOWS account (see 1)). You can also use your sa account.

Happy SQLing..

com.ibm.websphere.ce.cm.ObjectClosedException: DSRA9110E: ResultSet is closed.


While running the applications on the websphere we do encounter the error com.ibm.websphere.ce.cm.ObjectClosedException: DSRA9110E: ResultSet is closed.
There are many reasons for why we get this error and so do equal number of solutions. I have scenerio where the result set is shared by the class APIs and started encountering this error. One of the good solutions I implemented is setting the resultSetHoldability property from 2 to 1 (Please see the documentations of WebSphere for furthur details.) After changing this property value please restart the server for the property to reset. This is one of the GREATEST APP SERVER.

The stacktrace look something like this.
com.ibm.websphere.ce.cm.ObjectClosedException: DSRA9110E: ResultSet is closed.
at com.ibm.ws.rsadapter.jdbc.WSJdbcResultSet.runtimeXIfNotClosed(WSJdbcResultSet.java:2691)
at com.ibm.ws.rsadapter.jdbc.WSJdbcResultSet.next(WSJdbcResultSet.java:2478)
....

....
....
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:615)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:310)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy7.generateReport(Unknown Source)
.....

.....
at org.springframework.scheduling.commonj.DelegatingWork.run(DelegatingWork.java:61)
at com.ibm.ws.asynchbeans.J2EEContext.run(J2EEContext.java:1112)
at com.ibm.ws.asynchbeans.WorkWithExecutionContextImpl.go(WorkWithExecutionContextImpl.java:195)
at com.ibm.ws.asynchbeans.CJWorkItemImpl.run(CJWorkItemImpl.java:187)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1510)


The one of the solution for this is go to the admin console and select JDBC->Data Sources->Custom Properties->resultSetHoldability and change its value from 2 to 1.

Setup and Teardown

We all know the functionality of the init() method in the servlet. We also agree this is one of the best or a fantastic API of the servlet lifecycle. Spring provides the similar configurable support for the BEAN, but note this is not the callback API nor has a mandatory naming (reserve word). This also has a lot of difference in the servlet's init() and this configurable custom initilization method. I generally compare these two since I personaly feel they are similar and it help most of them.

Now lets come to the Spring, after the constructor is used to create the object of the class, we can configure a method to be called by the spring container (init() is called by the servlet container). After the constructor is done with its defined job and we need some other peice of code to invoked which might be used once in the life cycle of the object, we can configure this method as the init-method for this bean.

I found this very useful when we had a different encryption and decription keys for the objects and is independent to the intentions of the bean creation. So I configure this init-method and destroy-method to achieve this. Here is the typical code for the same.






package test.spring.injection;

public class CustomInit {
  
  public CustomInit(){
    System.out.println("This is the default constructor...");
  }
  
  public void setEncryptionKey(){
    System.out.println("Go to DB/Context load the key");
  }
  
  public void broadcastEncryptionKey(){
    System.out.println("Shout you are erased...");
  }

}






<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<beans>
  <bean id="customInit" class="test.spring.injection.CustomInit"
    init-method="setEncryptionKey"
    destroy-method="broadcastEncryptionKey" />
</beans>


The best approach then the above would be to implement the InitializingBean and DisposableBean. Now override the afterPropertiesSet() [implement init-method logic here] and destroy() [implement the destroy-method logic here]. These are the bean lifecycle API's

Spring DI (factory method)

Now that we understand the Spring Dependency Injection and the singleton implementation, and we also know how to configured the class as bean in spring definition file which has the constructor either the default or overridden. Let’s understand how to configure the singleton class in spring.
To highlight the scenario singleton does not have a public constructor (which means new keyword cannot be used to instantiate the object of this class) and has a public static getInstance method to get the instance of the class.
Spring provided the way to configure such classes as beans through the factory methods. Here we will configure the factory-method which is responsible to return the object of the class.

The singleton class would be


The singleton I read in the "Spring in Action" book
package test.patterns;

public class SingltonInner {

  private SingltonInner(){}
  
  private static class InnerSingltonInstance{
    static SingltonInner singlton = new SingltonInner();
  }
  
  public static SingltonInner getInstance(){
    return InnerSingltonInstance.singlton;
  }
  
  public String getMessage(String name){
    return "Hello " + name;
  }
}

Java2html

The class using the Singleton

package test.spring.injection;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import test.patterns.SingltonInner;

public class FactoryInjection {
  
  private SingltonInner singleton = null;
  
  public SingltonInner getSingleton() {
    return singleton;
  }

  public void setSingleton(SingltonInner singleton) {
    this.singleton = singleton;
  }
public public static void main(String arg[]){
        ApplicationContext ctx_1 = new ClassPathXmlApplicationContext("test//spring//injection//factory-injection.xml");
        FactoryInjection fInjection =(FactoryInjectionctx_1.getBean("factoryInjection");    
        System.out.println("Calling the singleton method :"+fInjection.singleton.getMessage("Java Om"));    
  }
}
Java2html


The definition of the beans is in this XML file.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
    <bean id="factoryInjection" class="test.spring.injection.FactoryInjection">
        <property name="singleton">
            <ref bean="singltonInner"/>
        </property>
    </bean>
    <bean id="singltonInner" class="test.patterns.SingltonInner" factory-method="getInstance"/>
</beans>

Singlton

While writing the Singleton class I would consider these things


  • Make the default constructor private.
  • Create a static and public getInstance method returning the object of the singleton class.
  • Make the class final.
  • Implement the Cloneable interface.
  • Override the clone method to throw the CloneNotSupportedException.

package test.patterns;
public final class Singlton implements Cloneable {

private static Singlton singlton = null;
private Singlton(){}

public static Singlton getInstance(){
    if(singlton == null){
        singlton = new Singlton();
    }
    return singlton;
}


    protected Object clone() throws CloneNotSupportedException {
        throw new CloneNotSupportedException ("I do not allow to clone, this is SINGLTON");
    }

}
used Java2html to format the code

The other good type I read in the "Spring in Action" book
package test.patterns;

public class SingltonInner {

  private SingltonInner(){}
  
  private static class InnerSingltonInstance{
    static SingltonInner singlton = new SingltonInner();
  }
  
  public static SingltonInner getInstance(){
    return InnerSingltonInstance.singlton;
  }
}









Java2html





Followers