Skip to main content

Technical

2023

Configure Jackson Object Mapper to Avoid Exception during Serializing a Lazy Loaded Hibernate Entity
·3 mins read
Mayukh Datta
Technical Spring Boot
Ah, the timeless classic in the world of software engineering – the production issue that decides to make its grand entrance on a Friday night. :police_car_light: One such incident happened last week. Turns out, the bug was in one of the features that I recently worked on. We are passing an entity to the Jackson’s Object Mapper to serialize it and return it as a JSON. The entity has some fields which are configured to lazy-load by Hibernate. Now, while serializing the entity we faced an unexpected behaviour. This was the bug that was breaking my API in production. However, I was a bit lucky here that this issue was confined to a particular scenario only. :face_with_tongue:
Dynamic Quartz Job Scheduling in Spring Boot
·4 mins read
Mayukh Datta
Technical Quartz Spring Boot
Quartz is a feature-rich open-source Java library used to schedule jobs. The core components of this library are Job, JobDetail, Trigger, and Scheduler. The JobDetail is where the job definition lies. The Trigger contains the instructions about when the job should get executed and Scheduler who runs the Job. Spring Boot Maven Dependency for Quartz library # <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency> The Core Components # Job: We create our own jobs by extending the QuartzJobBean class that implements Job interface. This interface has one method execute(JobExecutionContext context), which is overriden by the QuartzJobBean. This is where the scheduled task runs and the information on the JobDetail and Trigger is retrieved using the JobExecutionContext. The QuartzJobBean also gives us an abstract method executeInternal(JobExecutionContext context). Your Job class needs to override the executeInternal(...) method and should contain the code you want to get processed when the job gets executed. public class MyJob extends QuartzJobBean { ... @Override protected void executeInternal(@NonNull JobExecutionContext context) { ... } } JobDetail: Defining the Job. JobDetail jobDetail = JobBuilder.newJob(MyJob.class) .withIdentity(jobName) .storeDurably() .requestRecovery() .usingJobData(jobDataMap) .build(); Trigger: Defining the schedule upon which the job will be executed. Trigger jobTrigger = TriggerBuilder.newTrigger() .forJob(jobDetail) .startAt(scheduledAt) .withIdentity(triggerName) .build(); Scheduler: This is the main API for interacting with the Quartz Scheduler. We can get an instance of the Scheduler from the SchedulerFactoryBean. public class MyJob extends QuartzJobBean { @Autowired SchedulerFactoryBean schedulerFactoryBean; ... public void start(...) { Scheduler timer = schedulerFactoryBean.getScheduler(); timer.start(); ... timer.scheduleJob(jobDetail, jobTrigger); } Job Persistence # Quartz gives us JobStore for storing all the Job, Trigger and Scheduler related data.
Custom Logger Configuration for Feign Clients
·3 mins read
Mayukh Datta
Technical Spring Boot
Feign Client allows you to abstract the mechanics of calling a REST service. You need to configure and annotate the Feign Client interface to call a RESTful web service by making a simple Java method call.

2022

How to Export a Remote PostgreSQL Database and Import it locally?
·1 min read
Mayukh Datta
Technical SQL
Create a dump of the remote database using pg_dump command pg_dump --host=remote_host_name --port=remote_port_number --username=remote_database_user_name --dbname=database_name > db_export.sql You will get a dump SQL file of the remote database upon successful completion of this process. Create a database on your local setup with the same name as the remote database. psql --username=local_database_user_name -c 'create database database_name;’ The database has now been created on your local machine. Import the data to your local database. psql --username=local_database_user_name database_name < db_export.sql And, done!
How to Embed Code from any GitHub Repository (Not GitHub Gists) in WordPress?
·3 mins read
Mayukh Datta
Technical
GitHub Gists is a popular platform that offers us to save snippets of code and share them via a link as well as embed them in a webpage. But what if we want to embed code directly from a GitHub repository? I’ve written a script to embed code from a GitHub repository easily in your webpage. I have used the Prism library for syntax highlighting. The Script # <pre id="embed-github-pre"><code class="language-cpp line-numbers" id="embed-github-code"></code></pre> <script> let gitHubLink = "https://raw.githubusercontent.com/thecoducer/Competitive-Coding/master/LeetCode/two-sum-2.cpp"; getCodeAsRawTextFromGithub().then(data => { let preProcessedData = preProcessData(data); document.getElementById("embed-github-code").innerHTML = preProcessedData; loadScripts(["https://cdn.jsdelivr.net/gh/thecoducer/anything/prism.js"]); }); async function getCodeAsRawTextFromGithub() { let response = await fetch(gitHubLink); let rawText = await response.text(); return rawText; } function preProcessData(data) { data = data.replaceAll("<", "&lt;"); data = data.replaceAll(">", "&gt;"); return data; } function loadScripts(scripts) { for (let i = 0; i < scripts.length; i++) { this.insertAScriptTagInsideTheHeadTag(scripts[i]); } } function insertAScriptTagInsideTheHeadTag(src) { let scriptTag = document.createElement('script'); scriptTag.type = "text/javascript"; scriptTag.async = true; scriptTag.src = src; let headTag = document.getElementsByTagName('head')[0]; headTag.appendChild(scriptTag); } </script> How it works? # The value of gitHubLink should be the raw URL of the code file on GitHub that you want to embed.
Performance Optimization using Pivot Tables Technique in MySQL
·4 mins read
Mayukh Datta
Technical SQL
Making a database call is an expensive operation. It involves opening a connection to the database, and the SQL query passed through this connection to the database is parsed, optimized, and then finally executed. The process of opening a new connection to the database takes a significant amount of time and space. If we keep on opening new connections every time for each new request to our application, it would take a lot of time to send the response back and would also exceed our memory resources. To solve this problem, we use some connection pooling mechanisms to open/close, reuse and maintain connections to the database. Spring Boot by default uses HikariCP, which is a connection pooling tool. This means that the web application doesn’t have to face any delay in serving response due to managing database connections.
Clean Code: Write Methods with No Side-effects
·4 mins read
Mayukh Datta
Technical Java Spring Boot
I was listening to Uncle Bob Martin’s lecture on YouTube last night. His No ‘side-effects’ advice hit me and made me realize that I was doing something in my code that I shouldn’t be doing it that way.
Get Remote Server Logs of Your Spring Boot Application via REST API
·5 mins read
Mayukh Datta
Technical Java Spring Boot
I’m currently working on a microservices-based Spring Boot application, where I’m using Logback as my logging framework. The application is now running live on a remote server. Since I don’t have access to the server, I don’t get to see the logs when I want to see them or when there is some error. I have to ask someone from the sysadmin team to send me the log file every time and wait for their response. This kind of human dependency eats up a decent amount of time in any software development lifecycle. I try to avoid such dependencies and seek to circumvent them. So, I have written a simple snippet of code that can read the log file from the server filesystem and send it back to me in an API response.
When to Use Enums Instead of If-else or Switch Statements to Make the Code Cleaner?
·3 mins read
Mayukh Datta
Technical Java Spring Boot
We usually use if-else or switch statements to write our logical conditions in our code. There is nothing to worry about as long as your list of logical conditions is short.

2021

My #LearnbyDoing Journey with Crio.Do
·6 mins read
Mayukh Datta
Technical
I have now spent a week learning by doing with Crio.Do. It has been a wonderful week diving deep into software development essential areas like HTTP, REST APIs, Linux, AWS Cloud, and Git.

2020

My Interview Experience with Persistent Systems
·6 mins read
Mayukh Datta
Technical Interview Experience
I had applied at Persistent Systems through the AMCAT portal on 12th November 2020. It was a virtual off-campus hiring process. I had to go through an online test followed by two technical rounds and an HR round. Online Test # It was held on 19th November 2020. There were various questions divided into different sections like Computer Science fundamentals, English, Logical Aptitude, and Coding. The quality of the questions was good enough. The coding section consisted of two questions. One was a simple question on arrays. I wrote the solution in a minute and it passed all the test cases. The second question required to implement the KMP algorithm. I had to count the occurrences of a substring in a given string. My solution passed all the test cases.

2019

Made a diary app with Django
·2 mins read
Mayukh Datta
Technical Django
Django is a Python-based web framework. Most of the files that you will edit or create in your Django project will be Python files and even the settings file. Django extensively uses Python data structures like dictionaries, lists, and tuples. It has the Model View Template architecture and many cool features and built-in functionalities that can make your job of building a web app really easy. There are hundreds of apps built with Django and tons of libraries or components built to work with Django. They are all open-sourced. Therefore, you can use them in your own Django app to add certain functionalities. I used the social-auth-app-django to get the social login feature up on my app without having to write any code from scratch. The community is rich, you need to explore deep enough to get what you want. My interest in Django came after I started learning Python. I wanted to build a web app and knew there are two major web frameworks in Python: Flask and Django. I began learning Flask and made a To-do list app with it. Later on, when I started making inroads into Django I realized it to be better than Flask.