I’m trying to get my DataServices layer written and I’d like to get a code review. Here’s my method for adding a record.
/**
* addDataStore() inserts new <code>DataStore</code> into the database through Hibernate.
*
* @param DataStore A new <code>DataStore</code> to be added.
*/
public void addDataStore(DataStore DataStore) throws Exception
{
// Create a configuration based on the properties file we've put
// in the standard place.
Configuration config = new Configuration();
// Tell it about the classes we want mapped, taking advantage of
// the way we've named their mapping documents.
config.addClass(DataStore.class);
// Get the session factory we can use for persistence
SessionFactory sessionFactory = config.buildSessionFactory();
// Ask for a session using the JDBC information we've configured
Session session = sessionFactory.openSession();
Transaction tx = null;
try
{
// Begin transaction
tx = session.beginTransaction();
// Add new record
session.save(DataStore);
// Commit transaction
tx.commit();
}
catch (HibernateException e)
{
if (tx != null) {
tx.rollback();
}
log.error("Hibernate Exception" + e.getMessage());
throw new RuntimeException(e);
}
/*
* Regardless of whether the above processing resulted in an Exception
* or proceeded normally, we want to close the Hibernate session. When
* closing the session, we must allow for the possibility of a Hibernate
* Exception.
*
*/
finally
{
if (session != null)
{
try
{
session.close();
}
catch (HibernateException e)
{
log.error("Hibernate Exception" + e.getMessage());
throw new RuntimeException(e);
}
}
}
}
Thanks,
Lee