|
Hibernate example source code file (Transactions.xml)
The Hibernate Transactions.xml source code
<?xml version='1.0' encoding='utf-8' ?>
<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
<!ENTITY % BOOK_ENTITIES SYSTEM "Hibernate_Development_Guide.ent">
%BOOK_ENTITIES;
]>
<chapter>
<title>Transactions and concurrency control
<section>
<title>Defining Transaction
<para>
It is important to understand that the term transaction has many related, yet different meanings in relation
to persistence and Object/Relational Mapping. In most use cases these definitions align, but that is not
always the case.
</para>
<itemizedlist>
<listitem>
<para>
It might refer to the physical transaction with the database.
</para>
</listitem>
<listitem>
<para>
It might refer to the logical notion of a transaction as related to a persistence context.
</para>
</listitem>
<listitem>
<para>
It might refer to the application notion of a Unit-of-Work, as defined by the archetypal pattern.
</para>
</listitem>
<note>
<para>
This documentation largely treats the physical and logic notions of transaction as one-in-the-same.
</para>
</note>
</itemizedlist>
</section>
<section>
<title>Physical Transactions
<para>
Hibernate uses the JDBC API for persistence. In the world of Java there are 2 well defined mechanism
for dealing with transactions in JDBC: JDBC, itself, and JTA. Hibernate supports both mechanisms for
integrating with transactions and allowing applications to manage physical transactions.
</para>
<para>
The first concept in understanding Hibernate transaction support is the
<interfacename>org.hibernate.engine.transaction.spi.TransactionFactory interface which
serves 2 main functions:
</para>
<itemizedlist>
<listitem>
<para>
It allows Hibernate to understand the transaction semantics of the environment. Are we operating
in a JTA environment? Is a physical transaction already currently active? etc.
</para>
</listitem>
<listitem>
<para>
It acts as a factory for <interfacename>org.hibernate.Transaction instances which
are used to allow applications to manage and check the state of transactions.
<interfacename>org.hibernate.Transaction is Hibernate's notion of a logical
transaction. JPA has a similar notion in the
<interfacename>javax.persistence.EntityTransaction interface.
</para>
</listitem>
</itemizedlist>
<para>
<interfacename>org.hibernate.engine.transaction.spi.TransactionFactory is a
standard Hibernate service. The default initiator,
<classname>org.hibernate.engine.transaction.internal.TransactionFactoryInitiator,
looks for a <literal>hibernate.transaction.factory_class configuration setting to determine
the factory to use.
<!-- todo : see service registry appendix -->
</para>
<section>
<title>Physical Transactions - JDBC
<para>
JDBC-based transaction management leverages the JDBC defined methods
<methodname>java.sql.Connection.commit() and
<methodname>java.sql.Connection.rollback() (JDBC does not define and explicit
method of beginning a transaction). In Hibernate, this approach is represented by the
<classname>org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory class.
</para>
</section>
<section>
<title>Physical Transactions - JTA
<para>
JTA-based transaction management leverages the
<interfacename>javax.transaction.TransactionManager interface as obtained from
<interfacename>org.hibernate.service.jta.platform.spi.JtaPlatform API. This approach
is represented by the
<classname>org.hibernate.engine.transaction.internal.jta.JtaTransactionFactory class.
<!-- todo : see service registry appendix -->
</para>
</section>
<section>
<title>Physical Transactions - CMT
<para>
CMT-based transaction management leverages the
<interfacename>javax.transaction.UserTransaction interface as obtained from
<interfacename>org.hibernate.service.jta.platform.spi.JtaPlatform API. This approach
is represented by the
<classname>org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory class.
<!-- todo : see service registry appendix -->
</para>
</section>
<section>
<title>Physical Transactions - Custom
<para>
Its is also possible to plug in ones own transaction approach by implementing the
<interfacename>org.hibernate.engine.transaction.spi.TransactionFactory contract.
The default service initiator has built-in support for understanding custom transaction approaches
via the <literal>hibernate.transaction.factory_class which can name either:
</para>
<itemizedlist>
<listitem>
<para>
The instance of <interfacename>org.hibernate.engine.transaction.spi.TransactionFactory
to use.
</para>
</listitem>
<listitem>
<para>
The name of a class implementing
<interfacename>org.hibernate.engine.transaction.spi.TransactionFactory
to use. The expectation is that the implementation class have a no-argument constructor.
</para>
</listitem>
</itemizedlist>
</section>
<section>
<title>Physical Transactions - Legacy
<para>
During development of 4.0, most of these classes named here were moved to new packages. To help
facilitate upgrading, Hibernate will also recognize the legacy names here for a short period of time.
</para>
<itemizedlist>
<listitem>
<para>
<literal>org.hibernate.transaction.JDBCTransactionFactory is mapped to
<classname>org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory
</para>
</listitem>
<listitem>
<para>
<literal>org.hibernate.transaction.JTATransactionFactory is mapped to
<classname>org.hibernate.engine.transaction.internal.jta.JtaTransactionFactory
</para>
</listitem>
<listitem>
<para>
<literal>org.hibernate.transaction.CMTTransactionFactory is mapped to
<classname>org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory
</para>
</listitem>
</itemizedlist>
</section>
</section>
<section>
<title>Hibernate Transaction Usage
<para>
Hibernate uses JDBC connections and JTA resources directly, without adding any additional locking behavior.
It is important for you to become familiar with the JBDC, ANSI SQL, and transaction isolation specification
of your database management system.
</para>
<para>
Hibernate does not lock objects in memory. The behavior defined by the isolation level of your database
transactions does not change when you use Hibernate. The Hibernate
<interfacename>org.hibernate.Session acts as a transaction-scoped cache providing
repeatable reads for lookup by identifier and entity queries and not-reporting queries that return scalar
values.
</para>
</section>
<sidebar>
<para>
To reduce lock contention in the database, a database transaction needs to be as short as possible. Long
database transactions prevent your application from scaling to a highly-concurrent load. Do not hold a
database transaction open during end-user-level work, but open it after the end-user-level work is finished.
This is concept is referred to as <literal>transactional write-behind.
</para>
</sidebar>
<section>
<title>Transaction Scopes
<section id="session-per-operation">
<title>Session-per-operation
<subtitle>Antipatterns to avoid
<para>
<firstterm>Session-per-operation refers to the antipattern of opening and closing a
<classname>Session for each database call in a single thread. It is also an antipattern in terms of
database transactions. Group your database calls into a planned sequence. In the same way, do not auto-commit
after every SQL statement in your application. Hibernate disables, or expects the application server to disable,
auto-commit mode immediately. Database transactions are never optional. All communication with a database must
be encapsulated by a transaction. Avoid auto-commit behavior for reading data, because many small transactions
are unlikely to perform better than one clearly-defined unit of work, and are more difficult to maintain and
extend.
</para>
<para>
For more antipatterns to avoid, see <xref linkend="transaction-antipatterns" />.
</para>
</section>
<section>
<title>Session-per-request
<subtitle>The typical desired pattern
<para>
The most common pattern in a multi-user client/server application is
<firstterm>session-per-request. In this model, a client sends a request to the server, where the
Hibernate persistence layer is running. Hibernate opens a new Session, and all database operations are executed
in this unit of work. After the work is completed, and the server prepares the response for the client, the
session is flushed and closed. Use a single database transaction to serve the clients request, starting and
committing it when you open and close the Session. The relationship between the transaction and the Session is
one-to-one.
</para>
<para>
Hibernate provides built-in management of the <firstterm>current session to
simplify the session-per-request pattern. Start a transaction to process a server request, and end the transaction
before sending the response to the client. Common solutions include:
</para>
<itemizedlist>
<listitem>
<para>
ServletFilter
</para>
</listitem>
<listitem>
<para>
AOP interceptor with a pointcut on the service methods
</para>
</listitem>
<listitem>
<para>
A proxy/interception container
</para>
</listitem>
</itemizedlist>
<para>
An EJB container is a standardized way to implement cross-cutting aspects such as transaction demarcation on EJB
session beans, declaratively with CMT. If you use programmatic transaction demarcation, use the <xref
linkend="hibernate-transaction-api" />.
</para>
<para>
To access a current session to process the request, call method
<methodname>sessionFactory.getCurrentSession(). The returned
Other Hibernate examples (source code examples)Here is a short list of links related to this Hibernate Transactions.xml source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.