Reputation: 7404
I am experimenting with Hibernate
and log4j
. In the following code I have purposely commented out txn.commit()
to see what the outcome would be.
In the log messages it still is giving an indication that the data was inserted into my table, but nothing is inserted, what do the log messages mean?
Client Class:
public class HelloWorldClient {
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction txn = session.getTransaction();
try {
txn.begin();
Address address = new Address("19 Sample st", "Sample state", "1234");
Person person = new Person("Person1", address);
session.save(person);
//txn.commit();
} catch(Exception e) {
if(txn != null) { txn.rollback(); }
e.printStackTrace();
} finally {
if(session != null) {
System.out.println("------------------------PROGRAM COMPLETED-------------------------");
session.close();
}
}
}
}
Log Output:
00:25:51,270 DEBUG SQL:109 - insert into Person (city, street, zipcode, name) values (?, ?, ?, ?)
00:25:51,305 TRACE BasicBinder:81 - binding parameter [1] as [VARCHAR] - [Sample state]
00:25:51,306 TRACE BasicBinder:81 - binding parameter [2] as [VARCHAR] - [19 Sample st]
00:25:51,306 TRACE BasicBinder:81 - binding parameter [3] as [VARCHAR] - [1234]
00:25:51,307 TRACE BasicBinder:81 - binding parameter [4] as [VARCHAR] - [Person1]
------------------------PROGRAM COMPLETED-------------------------
Upvotes: 0
Views: 152
Reputation: 21153
A database transaction is basically like a scratch pad of notes. You can issue certain types of SQL statements within the context of a transaction, they're actually executed against the database but aren't going to be visible or become permanent until you issue a commit.
In your case, you started a transaction and called session.save, which generated the INSERT statement you showed. Because you commented out the commit, that statement was executed but never committed, thus rolled back when your session closed.
Upvotes: 1