David Botterill
2005-05-16 18:54:26 UTC
I have an application that uses McKoi and Hibernate. I'm converting
from an Access DB to the McKoi DB. My conversion class inserts fine
just using JDBC. However, when I try to use Hibernate to insert
records, the record does not get inserted. The problem seems to be
intermittent. I was able to get ONE insert to work. Here's some
relevant code.
Thanks!
-David
SessionHelper.java
...snip...
public class SessionHelper {
private static SessionFactory sessionFactory;
public static final ThreadLocal session = new ThreadLocal();
public static void init() throws ExceptionInInitializerError {
String amsHome = System.getProperty("AMS_HOME","c:\\data\\amsjava");
try {
Properties configProps = new Properties();
//configProps.put("hibernate.connection.driver_class","org.gjt.mm.mysql.Driver");
configProps.put("hibernate.connection.driver_class","com.mckoi.JDBCDriver");
configProps.put("hibernate.connection.url","jdbc:mckoi:local://" +
amsHome + "/database/db.conf");
configProps.put("hibernate.connection.username","*****");
configProps.put("hibernate.connection.password","*****");
//configProps.put("hibernate.dialect","net.sf.hibernate.dialect.MySQLDialect");
configProps.put("hibernate.dialect","org.hibernate.dialect.MckoiDialect");
configProps.put("hibernate.cglib.use_reflection_optimizer","false");
configProps.put("hibernate.show_sql","true");
Configuration config = new Configuration();
config.setProperties(configProps);
config.addClass(mil.utarng.ams.data.Academy.class);
config.addClass(mil.utarng.ams.data.Bed.class);
config.addClass(mil.utarng.ams.data.Colors.class);
config.addClass(mil.utarng.ams.data.HighSchool.class);
config.addClass(mil.utarng.ams.data.Position.class);
// Create the SessionFactory
sessionFactory = config.buildSessionFactory();
} catch(Exception e) {
throw new ExceptionInInitializerError(e);
}
}
public static Session currentSession() {
Session s=null;
try {
if(null == sessionFactory) {
SessionHelper.init();
}
s = (Session) session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
//s.connection().setAutoCommit(true);
session.set(s);
}
} catch(HibernateException he) {
System.err.println("HibernateException="+ he);
} catch(ExceptionInInitializerError eiie) {
System.err.println("ExceptionInInitializerError=" + eiie);
}
return s;
}
...snip...
Class where I'm doing the insert:
...snip...
private void addRecord() {
/**
* First we need to create a blank record.
*/
currentRecord = new Academy();
if(currentType.equals(DelegateType.DELEGATE)) {
currentRecord.setPosition(Position.DELEGATE);
this.cmbTitle.getModel().setSelectedItem(PositionFactory.getPosition(Position.DELEGATE));
}
clearFields(this.getContentPane());
this.txtLname.requestFocus();
this.cmbName.setSelectedItem(currentRecord);
this.txtLname.setBackground(Color.RED);
this.txtFname.setBackground(Color.RED);
}
...snip...
private void saveRecord() {
...snip...
Session currentSession = SessionHelper.currentSession();
try {
Transaction t = currentSession.beginTransaction();
currentSession.save(currentRecord);
currentSession.flush();
// currentSession.refresh(currentRecord);
t.commit();
} catch(HibernateException he) {
JOptionPane.showInternalMessageDialog(this,
"Error Saving Record! Write down the following
information and call support:\n" + he.getMessage(),
"Save Error!",JOptionPane.ERROR_MESSAGE);
}
...snip...
from an Access DB to the McKoi DB. My conversion class inserts fine
just using JDBC. However, when I try to use Hibernate to insert
records, the record does not get inserted. The problem seems to be
intermittent. I was able to get ONE insert to work. Here's some
relevant code.
Thanks!
-David
SessionHelper.java
...snip...
public class SessionHelper {
private static SessionFactory sessionFactory;
public static final ThreadLocal session = new ThreadLocal();
public static void init() throws ExceptionInInitializerError {
String amsHome = System.getProperty("AMS_HOME","c:\\data\\amsjava");
try {
Properties configProps = new Properties();
//configProps.put("hibernate.connection.driver_class","org.gjt.mm.mysql.Driver");
configProps.put("hibernate.connection.driver_class","com.mckoi.JDBCDriver");
configProps.put("hibernate.connection.url","jdbc:mckoi:local://" +
amsHome + "/database/db.conf");
configProps.put("hibernate.connection.username","*****");
configProps.put("hibernate.connection.password","*****");
//configProps.put("hibernate.dialect","net.sf.hibernate.dialect.MySQLDialect");
configProps.put("hibernate.dialect","org.hibernate.dialect.MckoiDialect");
configProps.put("hibernate.cglib.use_reflection_optimizer","false");
configProps.put("hibernate.show_sql","true");
Configuration config = new Configuration();
config.setProperties(configProps);
config.addClass(mil.utarng.ams.data.Academy.class);
config.addClass(mil.utarng.ams.data.Bed.class);
config.addClass(mil.utarng.ams.data.Colors.class);
config.addClass(mil.utarng.ams.data.HighSchool.class);
config.addClass(mil.utarng.ams.data.Position.class);
// Create the SessionFactory
sessionFactory = config.buildSessionFactory();
} catch(Exception e) {
throw new ExceptionInInitializerError(e);
}
}
public static Session currentSession() {
Session s=null;
try {
if(null == sessionFactory) {
SessionHelper.init();
}
s = (Session) session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
//s.connection().setAutoCommit(true);
session.set(s);
}
} catch(HibernateException he) {
System.err.println("HibernateException="+ he);
} catch(ExceptionInInitializerError eiie) {
System.err.println("ExceptionInInitializerError=" + eiie);
}
return s;
}
...snip...
Class where I'm doing the insert:
...snip...
private void addRecord() {
/**
* First we need to create a blank record.
*/
currentRecord = new Academy();
if(currentType.equals(DelegateType.DELEGATE)) {
currentRecord.setPosition(Position.DELEGATE);
this.cmbTitle.getModel().setSelectedItem(PositionFactory.getPosition(Position.DELEGATE));
}
clearFields(this.getContentPane());
this.txtLname.requestFocus();
this.cmbName.setSelectedItem(currentRecord);
this.txtLname.setBackground(Color.RED);
this.txtFname.setBackground(Color.RED);
}
...snip...
private void saveRecord() {
...snip...
Session currentSession = SessionHelper.currentSession();
try {
Transaction t = currentSession.beginTransaction();
currentSession.save(currentRecord);
currentSession.flush();
// currentSession.refresh(currentRecord);
t.commit();
} catch(HibernateException he) {
JOptionPane.showInternalMessageDialog(this,
"Error Saving Record! Write down the following
information and call support:\n" + he.getMessage(),
"Save Error!",JOptionPane.ERROR_MESSAGE);
}
...snip...