Callback mechanism for Hibernate database operations -- Callback

Source: Internet
Author: User

1: Generally, most of the codes used to access the database using the Hibernate Session are the same, as shown in the following two methods,

 
  1. // Query the Teacher operation
  2. Ublic Teacher getTeacher (Long id) throws DataAccessException {
  3. SessionSession=GetSession();
  4. TeacherTeacher=Null;
  5. Try {
  6. Teacher= (Teacher) session. get (Teacher. class, id );
  7. ...
  8. } Catch (HibernateException ex ){
  9. Throw convertHibernateAccessException (ex );
  10. } Catch (SQLException ex ){
  11. Throw convertJdbcAccessException (ex );
  12. } Catch (RuntimeException ex ){
  13. Throw ex;
  14. } Finally {
  15. Session. close ();
  16. Return teacher;
  17. }
 
  1. // Query Class operations
  2. Ublic classInfo getTeacher (Long id) throws DataAccessException {
  3. SessionSession=GetSession();
  4. ClassInfoClassInfo=Null;
  5. Try {
  6. ClassInfo= (ClassInfo) session. get (ClassInfo. class, id );
  7. ...
  8. } Catch (HibernateException ex ){
  9. Throw convertHibernateAccessException (ex );
  10. } Catch (SQLException ex ){
  11. Throw convertJdbcAccessException (ex );
  12. } Catch (RuntimeException ex ){
  13. Throw ex;
  14. } Finally {
  15. Session. close ();
  16. Return classInfo;
  17. }
The above two methods are incorrect. A large number of repeated codes may cause maintenance difficulties in the future. Here we can consider separating the specific business logic processing part, instead, it only obtains and releases public sessions.

And exception handling to form a public method, as shown in the following code,

 
  1. // Public method after encapsulation
  2. Ublic Object process (HibernateCallback action) throws DataAccessException {
  3. SessionSession=GetSession();
  4. ObjectObj=Null;
  5. Try {
  6. Obj=Action. DoInHibernate (session );
  7. } Catch (HibernateException ex ){
  8. Throw convertHibernateAccessException (ex );
  9. } Catch (SQLException ex ){
  10. Throw convertJdbcAccessException (ex );
  11. } Catch (RuntimeException ex ){
  12. Throw ex;
  13. }

 

  1. // Query the Teacher operation
  2. Public Teacher getTeacher (final Long id) throws DataAccessException {
  3. Return (Teacher) process (new HibernateCallback (){
  4. Public Object doInHibernate (Session session) throws HibernateException, SQLException {
  5. TeacherTeacher= (Teacher) session. get (Teacher. class, id );
  6. Return teacher;
  7. }
  8. });
  9. }
A callback is a service operation performed by the caller by calling the object provided by the caller. The callback mechanism is generally used in combination with the template method as a mode. The callback mechanism allows the application staff to focus only on business logic implementation,

Instead of processing some underlying general operations, the general operations and business logic can be separated. For example, when using a Callback mechanism in Java database operations, you do not need to worry about opening or closing database connections. You only need to implement relevant data access in Callback, this ensures that the database connection is always closed in time.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.