What is singleton pattern? Write an example singleton class?
A singleton class is a class which has only one instance per application. Rather per JVM. This design pattern is extensively used when user needs exactly one instance. For example facade objects, state objects or for global variables (as it permits lazy allocation and initialization which may not happen in all languages).
To make a class singleton, its constructor is made private or protected so that it can not be instantiated. Instead, a getInstance () method is written to return a new instance of that class if it doesn’t exist or return the reference of that object if it already exists. If it’s a multithreaded environment make sure to synchronize the getInstance() menthod.
Conventional way of writing a singleton class is :
public class Singleton {
private static Singleton INSTANCE = null;
// to prohibit instantiation
private Singleton() {
}
public static Singleton getInstance() {
if (INSTANCE == null) {
// lazy initialisation
INSTANCE = new Singleton();
}
return INSTANCE;
}
}
You can synchronise on getInstance() method for a multi threaded environment.
Another thread-safe Java programming language lazy-loaded solution is:
public class Singleton {
private Singleton() {}
// lazy initialisation
private static class SingletonHolder {
private final static Singleton INSTANCE = new Singleton();
}
public static Singleton getInstance() {
return SingletonHolder.INSTANCE;
}
}
Remember a singleton class is one instance per JVM. So if you have your application running in a cluster of servers. Every server will have its own JVM, therefore its own instance of singleton class. In this case, purpose of making a class singleton is not fulfilled. Hence this pattern should be used with care.
nested exception is java.lang.NoClassDefFoundError: org/apache/poi/hssf/usermodel /HSSFCellStyle
I am getting following exception while exporting jasper report in xls format.
org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoClassDefFoundError: org/apache/poi/hssf/usermodel/HSSFCellStyle org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:899) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:793) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:476) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:431) javax.servlet.http.HttpServlet.service(HttpServlet.java:690) javax.servlet.http.HttpServlet.service(HttpServlet.java:803) org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:265) org.acegisecurity.intercept.web.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:107) org.acegisecurity.intercept.web.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:72)
How you can force the garbage collection?
We can’t have forced garbage collection. System.gc( ) does not necessarily force a synchronous garbage collection. Instead, the gc( ) call is really a hint to the runtime that now is a good time to run the garbage collector. The runtime decides whether to execute the garbage collection at that time and what type of garbage collection to run.
nested exception is java.lang.NoClassDefFoundError: org/apache/poi/hssf/usermodel /HSSFCellStyle
I am getting following exception while exporting jasper report in xls format.
org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoClassDefFoundError: org/apache/poi/hssf/usermodel/HSSFCellStyle org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:899) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:793) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:476) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:431) javax.servlet.http.HttpServlet.service(HttpServlet.java:690) javax.servlet.http.HttpServlet.service(HttpServlet.java:803) org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:265)
What is an interface?
The interface keyword takes the abstract concept one step further. You could think of it as a “pure” abstract class. It allows the creator to establish the form for a class: method names, argument lists, and return types, but no method bodies. An interface can also contain fields, but these are implicitly static and final. An interface provides only a form, but no implementation. An interface says, “This is what all classes that implement this particular interface will look like.”
java.sql.SQLException: Io exception: Broken pipe
I getting following exception.
java.sql.SQLException: Io exception: Broken pipe at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:125) at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:162) at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:274) at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:319) at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:344) at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:148) at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32) at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:545) at java.sql.DriverManager.getConnection(DriverManager.java:525) at java.sql.DriverManager.getConnection(DriverManager.java:171) at com.ibatis.common.jdbc.SimpleDataSource.popConnection(SimpleDataSource.java:571) at com.ibatis.common.jdbc.SimpleDataSource.getConnection(SimpleDataSource.java:213) at com.ibatis.sqlmap.engine.transaction.jdbc.JdbcTransaction.init(JdbcTransaction.java:48) at com.ibatis.sqlmap.engine.transaction.jdbc.JdbcTransaction.getConnection(JdbcTransaction.java:89) at com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryForList(GeneralStatement.java:123) at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList(SqlMapExecutorDelegate.java:610) at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList(SqlMapExecutorDelegate.java:584) at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.queryForList(SqlMapSessionImpl.java:101) at com.ibatis.sqlmap.engine.impl.SqlMapClientImpl.queryForList(SqlMapClientImpl.java:78)