
Quick Contact
Java Tutorial
- What is Java?
- History of Java
- Hello Java Program
- Features of Java
- Basic Syntax
- Java Setup
- Data Types in Java
- Java Variables
- Operators in Java
- JVM
- Java If-else Statement
- Switch Case Statement
- Java Loops
- Do-While Loop
- Java While Loop
- Continue Statement
- Break Statement in Java
- Constructors in Java
- Oops Concept in Java
- Features of OOPs
- Inheritance
- Exeception handeling
- Aggregation (HAS-A relationship) in Java
- Method Overriding in Java
- Method Overloading
- Java Static Keyword
- Java This Keyword
- Java Final Keyword
- Polymorphism
- Static Binding and Dynamic Binding
- Abstract class in Java
- Access Modifiers in Java
- Difference between abstract class and interface
- Interface in Java
- Garbage Collection in Java
- Java Package
- Encapsulation
- Serialization and Deserialization in Java
- Java Inner Classes
- Java Applets
- Multithreading in Java
- Thread Priorities in Java
- Thread Creation
- Inter Thread Communication
- Wrapper Classes in Java
- Java Input Output
- Java AWT Introduction
- Java Layout Manager
- Java Layout Policy
- Java AWT Events
- Collection Framework
- Collection Framework List Interface
- Swing in Java
- Swing Utility Classes
- Swing Layout Managers
- Java JDBC
- Hibernate Framework Overview – Architecture and Basics
Springboot
- Spring Environment Setup
- Spring Boot CRUD REST API Project using IntelliJ IDEA | Postman | MySQL
- Dockerizing Spring Boot Application | Spring Boot Docker Tutorial
- spring-boot-restapidocumentation with swagger
- Spring Boot HttpClient Overview
- Apache HttpClient POST HTTP Request Example
- Apache HttpClient PUT HTTP Request Example
- Apache HttpClient DELETE HTTP Request Example
- Apache HttpClient HTML Form POST Request Example
- Spring Boot JSP Exampl
- Deploying Spring Boot WAR file with JSP to Tomcat
- Spring Boot Annotations
- Spring Core Annotations
- Spring MVC Annotations with Examples
- Spring Scheduling Annotations
- Spring - Java-based Container Configuration
- Spring Java Based Configuration Example
Hibernate
- Hibernate 5 hello world
- Hibernate- One to One Unidirectional Mapping Annotation Example
- Hibernate - Batch Processing
- Hibernate - Interceptors
- Hibernate 5 - Create, Read, Update and Delete (CRUD) Operations Example
- Hibernate Transaction Management
- Hibernate One to Many Unidirectional Mapping Example
- Hibernate One to Many Bidirectional Mapping Example
- Hibernate Many to Many Annotation Mapping Example
- Hibernate Primary KeyJoin Column
- Hibernate First Level Cache with Example
- Hibernate XML Configuration Example with Maven + Eclipse + MySQL Database
- Hibernate Java Configuration Example
- JPA 2 with Hibernate 5 Bootstrapping Example
- JPA and Hibernate Cascade Types
- Hibernate/JPA - Primary Key Generation
- Hibernate 5 - Enum Type Mapping Example
- Hibernate Component Mapping
- Hibernate Object States – Transient,Persistent and Detached
- Hibernate 5 - Save an Entity Example
- Hibernate 5 - Persist an Entity Example
- Hibernate 5 - saveOrUpdate() Method Example
- Hibernate 5 - get(), load() and byId() Method Examples
- Hibernate 5 - merge() Example
- Hibernate 5 - Delete or Remove an Entity Example
- Hibernate 5 - load() Method Example
- Hibernate Session Interface Methods
- Hibernate Session.clear() Method Example
- Introduction Of Java strutes to Architecture
- Struts 2 - Architecture
- Struts 2 - Configuration Files
- Struts 2 - Actions
- Struts 2 - Interceptors
- Struts 2 - Results & Result Types
- Struts 2 - Value Stack/OGNL
- Struts 2 - File Uploading
- Struts 2 - Database Access
- Struts 2 - Validations Framework
JAVA FX
- JavaFX Tutorial
- Introduction to JavaFX Pane
- JavaFX Popup
- JavaFX group
- JavaFX Controller
- JavaFX Gradient Color
- JavaFXAnchorPane
- JavaFXTabPane
- JavaFX Scene
- JavaFX Stage
- JavaFXWebView
- JavaFX Timeline
- JavaFX Timer
- JavaFX Image
- JavaFX Background
- JavaFX dialog
- JavaFX Font
- JavaFXTextArea
- JavaFXObservableList
- JavaFX GUI
- JavaFX FXML
- JavaFXEventHandler
- JavaFXGradle
- JavafxScrollpane
- JavaFXAPI
Hibernate Transaction Management
What is a transaction?
A transaction simply represents a unit of work. Generally speaking, a transaction is a set of SQL operations that need to be either executed all successfully or not at all.
A transaction can be described by ACID properties (Atomicity, Consistency, Isolation, and Durability).</p>
<h5>Let’s discuss what are ACID properties in detail.
ACID Properties
A database must satisfy the ACID properties (Atomicity, Consistency, Isolation, and Durability) to guarantee the success of a database transaction.
Atomicity:
Each transaction should be carried out in its entirety; if one part of the transaction fails, then the whole transaction fails.
Consistency:
The database should be in a valid state before and after the performed transaction.
Isolation:
Each transaction should execute in complete isolation without knowing the existence of other transactions.
Durability:
Once the transaction is complete, the changes made by the transaction are permanent (even in the occurrence of unusual events such as power loss).
Transaction Interface
In the hibernate framework, we have <i>Transaction</i> interface that defines the unit of work. It maintains abstraction from the transaction implementation (JTA, JDBC).
A transaction is associated with <i>Session and instantiated by calling session.<i>beginTransaction().</i></p>
<p>The methods of Transaction interface are as follows:</p>
<ul>
<li><i>void begin()</i> – starts a new transaction.</li>
<li><i>void commit()</i> – ends the unit of work unless we are in FlushMode.NEVER.</li>
<li><i>void rollback()</i> – forces this transaction to rollback.</li>
<li><i>voidsetTimeout(int seconds)</i> – it sets a transaction timeout for any transaction started by a subsequent call to begin on this instance.</li>
<li><i>booleanisAlive()</i> – checks if the transaction is still alive.</li>
<li><i>voidregisterSynchronization(Synchronization s)</i> – registers a user synchronization callback for this transaction.</li>
<li><i>booleanwasCommited()</i> – checks if the transaction is committed successfully.</li>
<li><i>booleanwasRolledBack()</i> – checks if the transaction is rolled back successfully.</li>
</ul>
<h5>HibernateTransaction Management CRUD Example</h5>
<p>The below CRUD source code snippet demonstrates the usage of Transactions in Hibernate applications.</p>
<p>Consider we have <i>Student</i> JPA entity, let’s persist student object within a transaction.</p>
<pre>
publicvoidinsertStudent() {
Transactiontransaction=null;
try (Sessionsession=HibernateUtil.getSessionFactory().openSession()) {
// start a transaction
transaction=session.beginTransaction();
Stringhql=”INSERT INTO Student (firstName, lastName, email) “+
“SELECT firstName, lastName, email FROM Student”;
Queryquery=session.createQuery(hql);
int result =query.executeUpdate();
System.out.println(“Rows affected: “+ result);
// commit transaction
transaction.commit();
} catch (Exception e) {
if (transaction !=null) {
transaction.rollback();
}
}
}
</pre>
<p>Consider we have <i>Student</i> JPA entity, let’s update student object within a transaction:</p>
<pre>
publicvoidupdateStudent(Student student) {
Transactiontransaction=null;
try (Sessionsession=HibernateUtil.getSessionFactory().openSession()) {
// start a transaction
transaction=session.beginTransaction();
// save the student object
Stringhql=”UPDATE Student set firstName= :firstName”+”WHERE id = :studentId”;
Queryquery=session.createQuery(hql);
query.setParameter(“firstName”, student.getFirstName());
query.setParameter(“studentId”, 1);
int result =query.executeUpdate();
System.out.println(“Rows affected: “+ result);
// commit transaction
transaction.commit();
} catch (Exception e) {
if (transaction !=null) {
transaction.rollback();
}
}
}
</pre>
<p>Let’s delete student where id = 1;</p>
<pre>
publicvoiddeleteStudent(int id) {
Transactiontransaction=null;
try (Sessionsession=HibernateUtil.getSessionFactory().openSession()) {
// start a transaction
transaction=session.beginTransaction();
// Delete a student object
Studentstudent=session.get(Student.class, id);
if (student !=null) {
Stringhql=”DELETE FROM Student “+”WHERE id = :studentId”;
Queryquery=session.createQuery(hql);
query.setParameter(“studentId”, id);
int result =query.executeUpdate();
System.out.println(“Rows affected: “+ result);
}
// commit transaction
transaction.commit();
} catch (Exception e) {
if (transaction !=null) {
transaction.rollback();
}
}
}
</pre>
<p>Let’s get student record from a database.</p>
publicStudentgetStudent(int id) { Transactiontransaction=null; Studentstudent=null; try (Sessionsession=HibernateUtil.getSessionFactory().openSession()) { // start a transaction transaction=session.beginTransaction(); // get an student object Stringhql=" FROM Student S WHERE S.id = :studentId"; Queryquery=session.createQuery(hql); query.setParameter("studentId", id); List results =query.getResultList(); if (results !=null&&!results.isEmpty()) { student= (Student) results.get(0); } // commit transaction transaction.commit(); } catch (Exception e) { if (transaction !=null) { transaction.rollback(); } } return student; }
Apply now for Advanced Java Training Course