Transaction essentials You don t (Web host sites) want to use this
Monday, September 17th, 2007Transaction essentials You don t want to use this example as a template in your own application, because you should hide the exception handling with generic infrastructure code. You can, for example, write a single error handler for RuntimeException that knows when and how to roll back a transaction. The same can be said about opening and closing a Session. We discuss this with more realistic examples later in the next chapter and again in chapter 16, section 16.1.3, The Open Session in View pattern. Hibernate throws typed exceptions, all subtypes of RuntimeException that help you identify errors: The most common HibernateException is a generic error. You have to either check the exception message or find out more about the cause by calling getCause() on the exception. A JDBCException is any exception thrown by Hibernate s internal JDBC layer. This kind of exception is always caused by a particular SQL statement, and you can get the offending statement with getSQL(). The internal exception thrown by the JDBC connection (the JDBC driver, actually) is available with getSQLException() or getCause(), and the database- and vendor-specific error code is available with getErrorCode(). Hibernate includes subtypes of JDBCException and an internal converter that tries to translate the vendor-specific error code thrown by the database driver into something more meaningful. The built-in converter can produce JDBCConnectionException, SQLGrammarException, LockAquisition- Exception, DataException, and ConstraintViolationException for the most important database dialects supported by Hibernate. You can either manipulate or enhance the dialect for your database, or plug in a SQLExceptionConverterFactory to customize this conversion. Other RuntimeExceptions thrown by Hibernate should also abort a transaction. You should always make sure you catch RuntimeException, no matter what you plan to do with any fine-grained exception-handling strategy. You now know what exceptions you should catch and when to expect them. However, one question is probably on your mind: What should you do after you ve caught an exception? All exceptions thrown by Hibernate are fatal. This means you have to roll back the database transaction and close the current Session. You aren t allowed to continue working with a Session that threw an exception.
If you are searching for cheap webhost for your web application, please visit MySQL5 Web Hosting services.