-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Throwing custom exceptions before a transaction commit
PostPosted: Tue Apr 25, 2006 2:25 am 
Newbie

Joined: Tue Apr 25, 2006 2:08 am
Posts: 7
Hi All

I have just started learning about Hibernate so forgive me if I my questions seam stupid in anyway.

I have started by creating a hibernate config file and a mapping file for a domain object Subscription.

hibernate.cfg.xml:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>

<property name="hibernate.connection.password">test</property>

<property name="hibernate.connection.url">jdbc:mysql://localhost/Test</property>
<property name="hibernate.connection.username">test</property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>

<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<property name="hibernate.hbm2ddl.auto">create</property>

<mapping resource="nl/test/virtualcommunity/domain/Subscription.hbm.xml" />
</session-factory>
</hibernate-configuration>

Subscription.hbm.xml:
<class name="SubscriptionType" table="Subscription_Type">
<id name="id" type="int" column="SUBSCRIPTION_TYPE_ID">
<generator class="native" />
</id>

<property name="name" column="NAME" type="string" />
<property name="costs" column="COSTS" type="double" />
<property name="timeToLive" column="TIME_TO_LIVE" type="int" />
</class>

I want to create a DAO to load/save/update information about a subscription. I have created this code:

SubscriptionManagerDAO:
public Subscription findSubscription(int id) throws ResourceNotFoundException {
// Start a hibernate session
Session session = HibernateUtil.getSessionFactory().getCurrentSession();

Transaction transaction = null;
Reseller result = null;

try {
transaction = session.beginTransaction();

Query query = session.createQuery("from Subscription s where s.id = :id");
query.setInteger("id", id);
List list = query.list();

if (list != null)
result = (Subscription) list.get(0);

session.getTransaction().commit();
} catch (HibernateException he) {
logger.error(he);

if (!Validator.isEmpty(transaction))
transaction.rollback();
}

if (result == null)
throw new ResourceNotFoundException("Subscription with id '" + id + "' not found");

return result;
}

As you can see I throw a custom ResourceNotFoundException if result is empty but this happens after the try catch. I would like to throw the exception in the try section after the empty check of the query list, like this:

if (list != null)
result = (Subscription) list.get(0);
else
throw new ResourceNotFoundException("Subscription with id '" + id + "' not found");

But then the transaction.commit() wont be called and if I understand Hibernate good enough transaction.commit() must be called to free the jdbc connection.

Has anybody got some tips on how this could be solved or have you got any comments on how the function in the dao is constructed.

Thanks in advance,
Martyn


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 25, 2006 10:28 am 
Regular
Regular

Joined: Fri Sep 09, 2005 11:35 am
Posts: 101
use a finally block for transaction commit or rollback


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.