TechnoBuzz

A Techno Blog, mainly about Java

Exception Scenarios

Its been a while since I chimed in on the topic of Exceptions:

Checked : (java.lang.Exception). This type of exception represents useful information about the operation of a legally specified request that the caller may have had no control over and that the caller needs to be informed about. With checked exceptions, Java language forces us to document all the anticipated ways in which control might exit a method:

Unchecked : (java.lang.RuntimeException). These are exceptions that occur as a result of programming errors that the program could not be expected to catch.

Now, it is time to talk about the Exception Handling capabilities with DAOs in the Spring Framework

(2.5)

SQL Exception Translatorsql exception can be translated to a DataAccessException using the translate method

ex1, ex2, ex3 , ex4

The method HibernateDaoSupport :: convertHibernateAccessException – convert the given HibernateException to an appropriate exception from the org.springframework.dao hierarchy.

SessionFactoryUtils : 
public static DataAccessException convertHibernateAccessException(HibernateException ex) {
		if (ex instanceof JDBCConnectionException) {
			return new DataAccessResourceFailureException(ex.getMessage(), ex);
		}
		if (ex instanceof SQLGrammarException) {
			return new InvalidDataAccessResourceUsageException(ex.getMessage(), ex);
		}
		if (ex instanceof DataException) {
			return new InvalidDataAccessResourceUsageException(ex.getMessage(), ex);
		}
		if (ex instanceof LockAcquisitionException) {
			return new CannotAcquireLockException(ex.getMessage(), ex);
		}
		if (ex instanceof ConstraintViolationException) {
			return new DataIntegrityViolationException(ex.getMessage(), ex);
		}
		if (ex instanceof JDBCException) {
			return new HibernateJdbcException((JDBCException) ex);
		}
		if (ex instanceof PropertyValueException) {
			return new DataIntegrityViolationException(ex.getMessage(), ex);
		}
		if (ex instanceof PersistentObjectException) {
			return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
		}
		if (ex instanceof TransientObjectException) {
			return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
		}
		if (ex instanceof ObjectDeletedException) {
			return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
		}
		if (ex instanceof QueryException) {
			return new HibernateQueryException((QueryException) ex);
		}
		if (ex instanceof UnresolvableObjectException) {
			return new HibernateObjectRetrievalFailureException((UnresolvableObjectException) ex);
		}
		if (ex instanceof WrongClassException) {
			return new HibernateObjectRetrievalFailureException((WrongClassException) ex);
		}
		if (ex instanceof NonUniqueResultException) {
			return new IncorrectResultSizeDataAccessException(ex.getMessage(), 1);
		}
		if (ex instanceof StaleObjectStateException) {
			return new HibernateOptimisticLockingFailureException((StaleObjectStateException) ex);
		}
		if (ex instanceof StaleStateException) {
			return new HibernateOptimisticLockingFailureException((StaleStateException) ex);
		}

		// fallback
		return new HibernateSystemException(ex);
	}

Hibernate Template

Advertisements

September 13, 2008 Posted by | Hibernate & ORM, J2EE, Open Source | 1 Comment