Posts

Java Thread Local Storage explained in easiest practical way

Image
One of the cool feature of Java that I like is ThreadLocal storage. ThreadLocal storage is explained in easiest way with practical example in this blog. ThreadLocal is a class in java.lang package which provides a way to store and access data of any type per thread, which means each thread will have it's separate instance of the data. During execution of a thread, one class can set data in ThreadLocal and other classes further down in the execution layer can access/read the data, as long as execution in within same thread. It is important to know that ThreadLocal data set by one thread is not visible or accessible in another thread. That the core concept of ThreadLocal storage. The way to use ThreadLocal is declare a static property of type ThreadLocal where T is type of the data we want to store. It's static so that it easily accessible in classes wherever needed. You can declare it public, private or whatever make sense in your usecase. Usually declaring it as...

How to enable JPA eclipselink logging in WAS Liberty

Image
When this goes well, you are happy but every developer hits situation when you need more logs and details to troubleshoot issue and determine root case. It happens many times when you work with JPA and thing don't goes as you expect. You really need to look under the hood what JPA is going, what SQL statements it's generating and executing. Environment: WAS Liberty 17.0.01, JAVA EE 7, JPA 2.1, EclipseLink 2.6.3 In above environment, to enable JPA logging including SQL statements, bind parameters and transaction details do following - Add following tow properties in persistence.xml <property name="eclipselink.logging.level" value="ALL"/> <property name="eclipselink.logging.parameters" value="true"/> Add following logging tag in server.xml <logging traceSpecification="eclipselink=all" maxFileSize="20" maxFiles="10"/> Redeploy application and recycle server. All done! T...

EhCache3 as JCache (JSR-107) Implementation with Cache Statistics

Image
If you are reading this you already know that caching is mechanism to store static data In-Memory for faster access and avoid expensive database calls or any kind of service calls to get the data. Read my other post to learn InMemory Caching using EhCache3 . There are many ways to implement cache in java based applications, starting from very simple Map based caching to Apache Common JCS , EhCache , Apache Ignite , Hazelcast , Oracle Coherence and many more. With so many implementations available, question is which one we should go for?  If you have to choose one from multiple options best bet is to look for standard. Standard because many people have put their mind together in it, better support, examples and guide available for reference plus expect that further upgrades/changes will not breaking changes. So for InMemory cache Standard Specification is JSR-107 .  You can read about it in more detail at JSR 107: JCACHE - Java Temporary Caching API . JCache speci...

EhCache3 In Memory Caching for Performance Improvement

Image
If you are reading this you already know that caching is mechanism to store static data In-Memory for faster access and avoid expensive database calls or any kind of service calls to get the data. There are many ways to implement cache in java based applications, starting from very simple Map based caching to Apache Common JCS , EhCache , Apache Ignite , Hazelcast , Oracle Coherence and many more. Each of them have pros and cons. I will show how to use EhCache for caching implementation EhCache3 Implementation For this example you need to have following jars in the class path - <dependency> <groupId>org.ehcache</groupId> <artifactId>ehcache</artifactId> <version>3.4.0</version> </dependency> <dependency> <!-- We need this because ehcache uses slf4j for logging --> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.25</version>...

Syntax highlighting and formating source code in blog

Image
There are many very enthusiastic bloggers and writers in many fields. I am writing this blog for blogger who write about Information Technology, source code, algorithm. When a blogger is writing I am sure he has good enough knowledge about the subject and write very good quality content on it. Along with content quality, presentation and formatting of the content is very important so that reader can easily read and understand the idea. When I started writing technical blogs involving source code, it was very challenging to present source code on the blog which easily readable with all standard code syntax highlighting. Without formatting, indentation and keyword highlighting its very difficult for a programmer / coder to understand the code logic. Code would need to copy paste code from blog to IDE to see it properly and understand. While searching I found some fantastic and very easy to use tool or ways to highlight source code in your blogs. Below are detail about the tools I li...