tailieunhanh - manning Hibernate in Action phần 6

Tham khảo tài liệu 'manning hibernate in action phần 6', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | 158 CHAPTER 5 Transactions concurrency and caching We aren t interested in the details of direct JDBC or JTA transaction demarcation. You ll be using these APIs only indirectly. Hibernate communicates with the database via a JDBC Connection hence it must support both APIs. In a stand-alone or web-based application only the JDBC transaction handling is available in an application server Hibernate can use JTA. Since we would like Hibernate application code to look the same in both managed and non-managed environments Hibernate provides its own abstraction layer hiding the underlying transaction API. Hibernate allows user extension so you could even plug in an adaptor for the CORBA transaction service. Transaction management is exposed to the application developer via the Hibernate Transaction interface. You aren t forced to use this API Hibernate lets you control JTA or JDBC transactions directly but this usage is discouraged and we won t discuss this option. The Hibernate Transaction API The Transaction interface provides methods for declaring the boundaries of a database transaction. See listing for an example of the basic usage of Transaction. Listing Using the Hibernate Transaction API Session session Transaction tx null try tx concludeAuction catch Exception e if tx null try catch HibernateException he log he and rethrow e throw e finally try catch HibernateException he throw he Understanding database transactions 159 The call to marks the beginning of a database transaction. In the case of a non-managed environment this starts a JDBC transaction on the JDBC connection. In the case of a managed environment it starts a new JTA transaction if there is no current JTA transaction or joins the existing current JTA transaction. This is all handled by Hibernate you shouldn t need to care about the implementation. The call to synchronizes .