我有一个像这样的主要方法
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();
}
}
}
在我的run方法中,我连接到db并运行一些hibernate查询来更新不同表中的数据。 main方法和run方法都在同一个Java文件中返回。当我运行此文件时,我收到以下异常
java.lang.NullPointerException at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:119) 在 org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75) 在 org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159) 在 org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131) 在 org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:77) 在 org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2283) 在 org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2279) 在 org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1748) 在 org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1788) 在 com.myfw.runtime.Application.getSessionFactory(Application.java:15) 在 com.myfw.runtime.ServiceExecutionContext.setup(ServiceExecutionContext.java:114) 在 com.myorg.customer.CustomerUtils.getServiceExecutionContextForCustomer(CustomerUtils.java:633) 在 com.myorg.utils.UpdateDataForDb.updateDataMethod(UpdateDataForDb.java:747) 在com.myorg.utils.UpdateDataForDb.run(UpdateDataForDb.java:736)at java.lang.Thread.run(Unknown Source)初始SessionFactory创建 failed.java.lang.NullPointerException线程“Thread-1”中的异常 java.lang.ExceptionInInitializerError at com.myfw.runtime.Application.getSessionFactory(Application.java:21) 在 com.myfw.runtime.ServiceExecutionContext.setup(ServiceExecutionContext.java:114) 在 com.myorg.customer.CustomerUtils.getServiceExecutionContextForCustomer(CustomerUtils.java:633) 在 com.myorg.utils.UpdateDataForDb.updateDataMethod(UpdateData.java:747) 在com.myorg.utils.UpdateDataForDb.run(UpdateData.java:736)at java.lang.Thread.run(未知来源)
只有当我使用线程运行方法时才出现此问题,如果我不使用线程并逐个调用多个dbs的updatedata方法,那么一切似乎都能正常工作。
我的代码中使用的所有方法如下:
public void run() {
updateDataMethod(dbHost);
return;
}
/ * *将db名称作为输入并在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();
}
}
/ ** *创建与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;
}
}
从hibernate.cfg.xml获取sessionfactory如下
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;
}
答案 0 :(得分:0)
同步getSessionFactory()
,因为它会抛出ExceptionInInitializerError
,这在处理多线程时是一个常见问题。
public synchronized 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;
}