I have a main method like this
public static void main(String[] args) {
String dbHosts = args[0];
String[] dbHostsArr = dbHosts .split(",");
for(String dbHost: dbHostsArr ){
try {
Thread t = new Thread(new UpdateDataForDb(dbHost));
t.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Inside my run method I connect to db and run some hibernate queries to update data in different tables. Both main method and run method are return in same Java file. When I run this file I am getting the following exception
java.lang.NullPointerException at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:119) at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131) at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:77) at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2283) at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2279) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1748) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1788) at com.myfw.runtime.Application.getSessionFactory(Application.java:15) at com.myfw.runtime.ServiceExecutionContext.setup(ServiceExecutionContext.java:114) at com.myorg.customer.CustomerUtils.getServiceExecutionContextForCustomer(CustomerUtils.java:633) at com.myorg.utils.UpdateDataForDb.updateDataMethod(UpdateDataForDb.java:747) at com.myorg.utils.UpdateDataForDb.run(UpdateDataForDb.java:736) at java.lang.Thread.run(Unknown Source) Initial SessionFactory creation failed.java.lang.NullPointerException Exception in thread "Thread-1" java.lang.ExceptionInInitializerError at com.myfw.runtime.Application.getSessionFactory(Application.java:21) at com.myfw.runtime.ServiceExecutionContext.setup(ServiceExecutionContext.java:114) at com.myorg.customer.CustomerUtils.getServiceExecutionContextForCustomer(CustomerUtils.java:633) at com.myorg.utils.UpdateDataForDb.updateDataMethod(UpdateData.java:747) at com.myorg.utils.UpdateDataForDb.run(UpdateData.java:736) at java.lang.Thread.run(Unknown Source)
This problem comes only when I run the methods using threads, If I dont use threads and call the updatedata method for multiple dbs one by one everything seems to work fine.
All methods used inside my code are given below :
public void run() {
updateDataMethod(dbHost);
return;
}
/* * Method which takes db name as input and update tables inside the db */
private void updateDataMethod(String dbHost) {
ServiceExecutionContext ctx = null;
try {
ctx = CustomerUtils.getServiceExecutionContextForCustomer(dbHost);
System.out.println("ctx for "+dbHost);
if(ctx != null){
// different methods to insert data into tables
ctx.tearDownNormal();
}
}
catch(Exception e){
e.printStackTrace();
if(ctx != null)
ctx.tearDownException();
}
}
/** * Creates connection to db */
public static ServiceExecutionContext getServiceExecutionContextForCustomer(String dbHost) {
CustomerInfoThreadLocal.setDBHost(dbHost);
ServiceExecutionContext ctx = null;
try {
ctx = new ServiceExecutionContext(null);
ctx.setTransactionMode(ServiceExecutionContext.READWRITE);
ctx.setup();
} catch (Exception e) {
e.printStackTrace();
return null;
}
return ctx;
}
public void setup() throws Exception
{
try
{
System.out.println("Hibernate session setup started");
if (session == null)
session = Application.getInstance().getSessionFactory().openSession();
tx = session.beginTransaction();
if (this.transactionMode == ServiceExecutionContext.READWRITE)
session.setFlushMode(FlushMode.AUTO);
else
if (this.transactionMode == ServiceExecutionContext.READWRITECOMMITAFTER)
session.setFlushMode(FlushMode.COMMIT);
else
session.setFlushMode(FlushMode.MANUAL);
System.out.println("Hibernate session setup done");
} catch (Exception e)
{
throw e;
}
}
Get sessionfactory from hibernate.cfg.xml is as follows
public SessionFactory getSessionFactory() throws Exception
{
if (sessionFactory == null)
{
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure().buildSessionFactory();
}
catch (Throwable ex) {
ex.printStackTrace();
throw new ExceptionInInitializerError(ex);
}
}
return sessionFactory;
}