Home Navigation

Sunday 27 November 2016

Life cycle of a Thread (Thread States)

There are 4 states in thread life cycle in java new, runnable, non-runnable and terminated. There is no running state.

New: The thread is in new state if you create an instance of Thread class but before the invocation of start() method.

Runnable: The thread is in runnable state after invocation of start() method, but the thread scheduler has not selected it to be the running thread.

Running: The thread is in running state if the thread scheduler has selected it.

Non-Runnable (Blocked) : This is the state when the thread is still alive, but is currently not eligible to run.

Terminated: A thread is in terminated or dead state when its run() method exits.

Wednesday 16 November 2016

DB Connection issue, StaleConnectionException: null DSRA0010E: SQL State = 56038, Error Code = -4,743

StackTrace:

aused by: com.ibm.websphere.ce.cm.StaleConnectionException: null DSRA0010E: SQL State = 56038, Error Code = -4,743
       at com.ibm.ws.rsadapter.spi.ServerFunction.handleStaleStatement(ServerFunction.java:695)
       at com.ibm.ws.rsadapter.AdapterUtil.mapException(AdapterUtil.java:2279)
       at com.ibm.ws.rsadapter.AdapterUtil.translateSQLException(AdapterUtil.java:1584)
       at com.ibm.ws.rsadapter.spi.WSRdbDataSource.getConnection(WSRdbDataSource.java:2374)
       at com.ibm.ws.rsadapter.spi.WSManagedConnectionFactoryImpl.getConnection(WSManagedConnectionFactoryImpl.java:1782)
       at com.ibm.ws.rsadapter.spi.WSManagedConnectionFactoryImpl.createManagedConnection(WSManagedConnectionFactoryImpl.java:1551)
       at com.ibm.ws.rsadapter.spi.WSManagedConnectionFactoryImpl.createManagedConnection(WSManagedConnectionFactoryImpl.java:1107)
       at com.ibm.ejs.j2c.FreePool.createManagedConnectionWithMCWrapper(FreePool.java:2160)
       at com.ibm.ejs.j2c.FreePool.createOrWaitForConnection(FreePool.java:1838)
       at com.ibm.ejs.j2c.PoolManager.reserve(PoolManager.java:3809)
       at com.ibm.ejs.j2c.PoolManager.reserve(PoolManager.java:3085)
       at com.ibm.ejs.j2c.ConnectionManager.allocateMCWrapper(ConnectionManager.java:1548)
       at com.ibm.ejs.j2c.ConnectionManager.allocateConnection(ConnectionManager.java:1031)
       at com.ibm.ws.rsadapter.jdbc.WSJdbcDataSource.getConnection(WSJdbcDataSource.java:644)
       at com.ibm.ws.rsadapter.jdbc.WSJdbcDataSource.getConnection(WSJdbcDataSource.java:611)
       at com.workbrain.tool.jdbc.safe.SafeDataSource.getConnection(SafeDataSource.java:52)
       at com.workbrain2.platform.tool.jdbc.proxy.ProxyDataSource.getConnection(ProxyDataSource.java:96)
       at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
       at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
       ... 138 more
---- Begin backtrace for Nested Throwables
java.sql.SQLException: null DSRA0010E: SQL State = 56038, Error Code = -4,743
       at com.ibm.db2.jcc.am.gd.a(gd.java:752)
       at com.ibm.db2.jcc.am.gd.a(gd.java:66)
       at com.ibm.db2.jcc.am.gd.a(gd.java:135)
       at com.ibm.db2.jcc.am.so.c(so.java:2763)
       at com.ibm.db2.jcc.am.so.d(so.java:2751)
       at com.ibm.db2.jcc.am.so.a(so.java:2200)
       at com.ibm.db2.jcc.am.so.a(so.java:2176)
       at com.ibm.db2.jcc.t4.ab.h(ab.java:136)
       at com.ibm.db2.jcc.t4.ab.b(ab.java:41)
       at com.ibm.db2.jcc.t4.o.a(o.java:32)
       at com.ibm.db2.jcc.t4.tb.i(tb.java:145)
       at com.ibm.db2.jcc.am.so.ib(so.java:2169)
       at com.ibm.db2.jcc.am.so.a(so.java:3248)
       at com.ibm.db2.jcc.am.so.a(so.java:691)
       at com.ibm.db2.jcc.am.Connection.getJccSpecialRegisterProperties(Connection.java:8123)
       at com.ibm.db2.jcc.am.hf.getJccSpecialRegisterProperties(hf.java:1044)
       at com.ibm.ws.rsadapter.dbutils.impl.DB2UniversalUtilityImpl.getJccSpecialRegisterProperties(DB2UniversalUtilityImpl.java:386)
       at com.ibm.ws.rsadapter.spi.InternalDB2UniversalDataStoreHelper.addDefaultHPExtendedProperties(InternalDB2UniversalDataStoreHelper.java:2046)
       at com.ibm.ws.rsadapter.spi.WSRdbDataSource.getConnection(WSRdbDataSource.java:2322)
       at com.ibm.ws.rsadapter.spi.WSManagedConnectionFactoryImpl.getConnection(WSManagedConnectionFactoryImpl.java:1782)
       at com.ibm.ws.rsadapter.spi.WSManagedConnectionFactoryImpl.createManagedConnection(WSManagedConnectionFactoryImpl.java:1551)
       at com.ibm.ws.rsadapter.spi.WSManagedConnectionFactoryImpl.createManagedConnection(WSManagedConnectionFactoryImpl.java:1107)
       at com.ibm.ejs.j2c.FreePool.createManagedConnectionWithMCWrapper(FreePool.java:2160)
       at com.ibm.ejs.j2c.FreePool.createOrWaitForConnection(FreePool.java:1838)
       at com.ibm.ejs.j2c.PoolManager.reserve(PoolManager.java:3809)
       at com.ibm.ejs.j2c.PoolManager.reserve(PoolManager.java:3085)
       at com.ibm.ejs.j2c.ConnectionManager.allocateMCWrapper(ConnectionManager.java:1548)
       at com.ibm.ejs.j2c.ConnectionManager.allocateConnection(ConnectionManager.java:1031)
       at com.ibm.ws.rsadapter.jdbc.WSJdbcDataSource.getConnection(WSJdbcDataSource.java:644)
       at com.ibm.ws.rsadapter.jdbc.WSJdbcDataSource.getConnection(WSJdbcDataSource.java:611)
       at com.workbrain.tool.jdbc.safe.SafeDataSource.getConnection(SafeDataSource.java:52)
       at com.workbrain2.platform.tool.jdbc.proxy.ProxyDataSource.getConnection(ProxyDataSource.java:96)
       at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
       at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
       at com.workbrain2.platform.util.WbDataSourceUtil.getConnection(WbDataSourceUtil.java:74)
       at com.workbrain2.platform.svc.DbServer.impl.DbServerImpl.init(DbServerImpl.java:63)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:95)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:56)
       at java.lang.reflect.Method.invoke(Method.java:620)
       at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1702)
       at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1641)
       at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
       at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
       at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
       at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
       at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
       at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
       at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
       at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
       at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
       at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
       at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
       at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
       at com.workbrain2.platform.core.context.WbClassPathXmlApplicationContext.<init>(WbClassPathXmlApplicationContext.java:39)
       at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
       at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:86)
       at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:58)
       at java.lang.reflect.Constructor.newInstance(Constructor.java:542)
       at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)

Solutions: Change the value for the property validateNewConnection to : true. Stop and restart the server.

Thursday 10 November 2016

Switch statement, copy in clipboard and interactive mode in windows command batch script

Interactive area to create a choice:

SETLOCAL ENABLEEXTENSIONS
SET /P M=Type 1, 2, 3, or 4 then press ENTER:



Switch statement Area:

2>NUL CALL :CASE_%M%

:CASE_1
    Do your stuff
:CASE_2
    Do your stuff
:CASE_3
    Do your stuff
:DEFAULT_CASE
    Do your stuff
:EOF

Copy Clip board:

SET "PASS=HELLO"
echo | SET /p=%PASS%|CLIP.EXE

Wednesday 9 November 2016

Graph representations

Graph is one of the most important data structure in computer science. There are several ways to represent graphs, each with its advantages and disadvantages. Some situations, or algorithms that we want to run with graphs as input, call for one representation, and others call for a different representation. Here, we'll see three ways to represent graphs.

Edge lists: One simple way to represent a graph is just a list, or array, of |E|∣E∣ edges, which we call an edge list. Since each edge contains just two or three numbers, the total space for an edge list is Theta Θ(E). For example, here's how we represent an edge list in JavaScript for the social network graph:

[ [0,1], [0,6], [0,8], [1,4], [1,6], [1,9], [2,4], [2,6], [3,4], [3,5],[3,8], [4,5], [4,9], [7,8], [7,9] ]

Edge lists are simple, but if we want to find whether the graph contains a particular edge, we have to search through the edge list. If the edges appear in the edge list in no particular order, that's a linear search through |∣E∣ edges.

Adjacency matrices: For a graph with |V| vertices, an adjacency matrix is a |V| times ∣V∣×∣V∣ matrix of 0s and 1s, where the entry in row i and column j is 1 if and only if the edge (i,j)(i,j) is in the graph.

[ [0, 1, 0, 0, 0, 0, 1, 0, 1, 0],
  [1, 0, 0, 0, 1, 0, 1, 0, 0, 1],
  [0, 0, 0, 0, 1, 0, 1, 0, 0, 0],
  [0, 0, 0, 0, 1, 1, 0, 0, 1, 0],
  [0, 1, 1, 1, 0, 1, 0, 0, 0, 1],
  [0, 0, 0, 1, 1, 0, 0, 0, 0, 0],
  [1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
  [1, 0, 0, 1, 0, 0, 0, 1, 0, 0],
  [0, 1, 0, 0, 1, 0, 0, 1, 0, 0] ]

Advantages
  • we can find out whether an edge is present in constant time

Disadvantages:
  •  First, it takes Theta(V^2) space. In other words, for a sparse graph, the adjacency matrix is mostly 0s, and we use lots of space to represent only a few edges
  • Second, if you want to find out which vertices are adjacent to a given vertex ii, you have to look at all ∣V∣ entries in row ii, even if only a small number of vertices are adjacent to vertex ii.

For an undirected graph, the adjacency matrix is symmetric: the row ii, column jj entry is 1 if and only if the row jj, column ii entry is 1. For a directed graph, the adjacency matrix need not be symmetric.

Adjacency lists: An array of linked lists is used. Size of the array is equal to number of vertices. Let the array be array[]. An entry array[i] represents the linked list of vertices adjacent to the ith vertex. This representation can also be used to represent a weighted graph. The weights of edges can be stored in nodes of linked lists
Vertex numbers in an adjacency list are not required to appear in any particular order, though it is often convenient to list them in increasing order, as in this example.

[ [1, 6, 8],
  [0, 4, 6, 9],
  [4, 6],
  [4, 5, 8],
  [1, 2, 3, 5, 9],
  [3, 4],
  [0, 1, 2],
  [8, 9],
  [0, 3, 7],
  [1, 4, 7] ]

We can get to each vertex's adjacency list in constant time, because we just have to index into an array. To find out whether an edge (i,j) is present in the graph, we go to i's adjacency list in constant time and then look for j in i's adjacency list. How long does that take in the worst case? The answer is Θ(d), where dd is the degree of vertex i, because that's how long i's adjacency list is.

for (var j = 0; j < graph[i].length; j++) {
    doStuff(graph[i][j]);
}