Thursday, March 11, 2021

Application hangs shortly after application restart on Tomcat

 https://community.pega.com/support/support-articles/application-stops-working-shortly-after-jvm-starts


Stuck thread trace looks similar to :

prio=5 os_prio=0 tid=0x00000000241f6000 nid=0x4dc waiting on condition [0x000000005173d000]
   java.lang.Thread.State: WAITING (parking)
    at sun.misc.Unsafe.park(Native Method)
    - parking to wait for  <0x00000006c336f458> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
    at java.util.concurrent.locks.LockSupport.park(Unknown Source)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(Unknown Source)
    at org.apache.tomcat.dbcp.pool2.impl.LinkedBlockingDeque.takeFirst(LinkedBlockingDeque.java:590)
    at org.apache.tomcat.dbcp.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:424)
    at org.apache.tomcat.dbcp.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:345)
    at org.apache.tomcat.dbcp.dbcp2.PoolingDataSource.getConnection(PoolingDataSource.java:134)
    at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1563)
    at


Root Cause

Apache Tomcat connection pool was used.


Use Tomcat JDBC connection instead of Apache Tomcat:
 

      <Resource name="applicationName"    

                    auth="Container"

                    type="javax.sql.DataSource"

                    factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"

                    maxActive="100"

                    maxIdle="30"

                    maxWait="10000"

                    driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"

Thursday, August 13, 2020

Useful Linux commands to quickly analyze a log

 1)   Limit the output to 'n' characters per line in a search .

In the above example, it is limited to 400 bytes.

cat sample_file.log | cut -b 1-400 | less


2) In a large file, count the total lines starting with ^1 in the first column

 sed '1 d' FILE.csv | cut -d',' -f1 | sort | grep '^1*' | wc -l

sed '1 d' - delete the first line i.e. the header generally in a CSV

FILE.csv - File in question to analyze

cut -d',' -f1 -  Split the file using the ',' as a separator ('d','), and then retain the first column (f1)

sort - order the output in ascending manner

wc -l - total number of lines in the file

Tuesday, January 7, 2020

Python debugging

Some tips for debugging in python :

1) Always use an editor to modify code (VS code in my case). Python is very strict about indentation, and if you mess it up, it will complain and complain!

2) Two golden lines to help you debug :

import pdb
pdb.set_trace()

Add this into each file, at whatever position you want it to stop in the pdb interactive debugger.

3) Commands for debugging
l - prints a snippet of the lines of code before and after the current line.
n - to step over next function
s - to step into next function
c - to continue execution
enter key - to execute previous command
pp - to pretty print the object
p - to print
where - to print the stack trace during execution (so very useful)

Happy debugging!

Thursday, November 28, 2019

Removing duplicate records from a hibernate query, while keeping pagination in mind

I was faced with a task, where I had to remove duplicate records from the result of a hibernate query, such that pagination parameters had to be applied only after the duplicate records were removed.

The following code snippet, used a single criteria object to do that.

https://stackoverflow.com/questions/11038234/pagination-with-hibernate-criteria-and-distinct-root-entity/23618190#23618190

What you want for each pagination page is 2 things, the total count of all results and your single page of results, but to do that you need to take 3 steps. 1) get the total count, 2) get the unique ids for your page, and 3) get the full data for the ids found in step 2. And you can do all that with a single criteria object:
1) get total count, using distinct ids (uniqueField = the name of your id in the entity class)
  Criteria criteria = session.createCriteria(YourEntity.class);
  Projection idCountProjection = Projections.countDistinct(uniqueField);
  criteria.setProjection(idCountProjection);
  //setup criteria, joins etc here
  int totalResultCount = ((Long)criteria.uniqueResult()).intValue();
2) reset projection and set start and length (you want the distinct ids)
  criteria.setProjection(Projections.distinct(Projections.property(uniqueField)));
  criteria.setFirstResult(start); 
  criteria.setMaxResults(length);
  List uniqueSubList = criteria.list();
3) reset projection and get distinct results that match ids

  criteria.setProjection(null);
  criteria.setFirstResult(0); criteria.setMaxResults(Integer.MAX_VALUE);
  criteria.add(Restrictions.in(uniqueField, uniqueSubList));
  criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
  List searchResults = criteria.list();
  //and now, however you want to return your results
  Map<String, Object> searchResultsMap = new HashMap<String, Object>();
  searchResultsMap.put("searchResults", searchResults);
  searchResultsMap.put("totalResultCount", totalResultCount);

Friday, September 20, 2019

Javascript debugging made easy


1) console.trace()
1.1)  Use console.trace() to figure out the entire stack trace from where a function call was made

2) console.group(label)
2.1) Use console.group(label) and console.groupEnd() to group the log statements together.
Be sure to specify the label i.e. the group name, so that it is easily readible amongst the verbose browser output.

3) Conditional Breakpoints
3.1) USE CONDITIONAL breakpoints! Makes life so much simpler.

4) Console.dir instead of console.log

May 31, 2019 - The output is presented as a hierarchical listing with disclosure triangles that let you see the contents of child objects. In other words, console.dir() is the way to see all the properties of a specified JavaScript object in console by which the developer can easily get the properties of the object.

5) Use console.table() to output an object in tabular format to the console.
Also, here are other super useful articles :
https://medium.com/appsflyer/10-tips-for-javascript-debugging-like-a-pro-with-console-7140027eb5f6


https://medium.com/datadriveninvestor/stopping-using-console-log-and-start-using-your-browsers-debugger-62bc893d93ff

https://medium.com/@mattburgess/beyond-console-log-2400fdf4a9d8

https://medium.com/better-programming/boost-your-javascript-debugging-skills-with-these-console-tricks-ab984c70298a

https://medium.com/better-programming/how-to-find-bugs-in-your-code-with-the-debugger-a7f739ea98

6) Turn on the view timestamp setting in the browser console.
7) Use console.error() and console.warn() instead of console.log() so the logged content is easily identifiable because it will be displayed in red and yellow colour respectively.