I have a storm topology to write some data from Kafka queue to Cassandra DB. The program is a multithreaded one. To facilitate the cassandra db insertion, I have this as my DBUtils:
public DBUtils() {
if(session == null) {
session = CassandraUtil.getInstance().getSession();
LOG.info("Started a new session for dbUtils-Monitoring.....");
}
synchronized(session) {
testMapper = new MappingManager(session).mapper(TestVO.class);
}
}
So, I have used synchronized to create a single dbUtils instance throughout all the running threads. But when I checked the logs, it seems like the session is initializing multiple times. The dbUtils in the storm topology was initialized in prepare method only and it has been used across prepare/execute/clean up methods. So, not sure how to use synchronized block if the variable I want to use throughout all the threads is used in multiple places. My question is then how to make the initialization of session/dbUtils variable only once throughout all the threads.