Wednesday, December 28, 2016

Beautiful Code

Is coding an art or a science? This question is pretty similar to the question when you see a beautiful bridge or a building. Is it art or science? Think about this. Build a technically superior bridge but not tastefully done. It will not draw the same kind of emotion. 

Saturday, December 17, 2016

Passing Spring properties file in command line

Normally we bake the file in Spring which is not a good idea for all the properties. It's better to externalize them so that the environment variables like database connection details can be passed to the Spring application from outside

For that start your Spring based application with the following command

java -jar application.jar

The properties file need to be present in the location from where you are firing the command.

Creating EntityManagerFactory in Spring

To create JPA based EntityManagerFactory backed by Hibernate implementation in Spring which gets auto configured, follow the code

//package containing the mapped model
@EnableJpaRepositories(basePackages = "com.lalit.example.Model")
public class DatabaseConfiguration {
        //You can autowire env which contains values from properties file or you
        //can inject properties directly using $
Environment env;

        //This will also create a pool of connections
public DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
return dataSource;


public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
// Put all the Hibernate properties
Properties additionalProperties = new Properties();
additionalProperties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
additionalProperties.put("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
additionalProperties.put("", env.getProperty(""));

LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();

JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); entityManagerFactory.setJpaVendorAdapter(vendorAdapter);


return entityManagerFactory;

        //Hook the transaction manager
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
return transactionManager;

        //Hook the exception translator
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();


Sunday, August 28, 2016

Pandas : Plotting a data series

A common need is to plot the data using pandas dataframe having different series data. For example, consider the following data:

Student, Class, Marks
A,       1,     56
A,       2,     67
A,       3,     89
A,       4,     76
A,       5,     76
B,       1,     78
B,       2,     99
B,       3,     75
B,       4,     44
B,       5,     77

Python finding the encoding of a file

chardet is a universal character encoding detector in python. It can find the encoding of a file also provides a confidence score of the encoding. To have chardet in your environment, install chardet with your package manager. With pip it would look like

pip install chardet

Tuesday, June 7, 2016

Serverless Architecture

The first thing to understand about serverless architecture is that it's not about the absence of server. What it means is that as a developer you are not concerned with server. You provide a code piece to an environment and it will be executed and results will be returned to you. You are just responsible for providing the code piece and generally the code piece has to adhere to some contract, so that the execution environment can understand it. AWS Lambda is an example of serverless architecture. 

Monday, June 6, 2016

Streaming data from Result Set

This example shows a way to stream the database records in a JSON format. This post is done against postgres db and a table is assumed. However you can point to any table structure and db after doing required adjustments.

public class StreamingService {

  public void handleRequest(String sql, OutputStream op) throws IOException 
   //Initialize the driver
   try {
   } catch (Exception e) {