Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate Without Transactions

 
Khadija Lokhandwala
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone,
I am using Hibernate 3.0, jdk 1.5,MySQL and Jrun
I wanted to know whether we can store/retrieve update data without using the following piece of code in hibernate

session.beginTransaction();

I am attaching the hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/oasysDB</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>

<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<!--Enabling Autocommit -->
<property name="connection.autocommit">true</property>

<property name="transaction.factory_class">org.hibernate.transaction.CMTTransactionFactory</property>
<property name="transaction.manager_lookup_class">org.hibernate.transaction.JRun4TransactionManagerLookup</property>
<property name="hibernate.transaction.flush_before_completion">true</property>
<property name="hibernate.transaction.auto_close_session">true</property>
<property name="hibernate.connection.release_mode">after_transaction</property>
<!-- Drop and re-create the database schema on startup
<property name="hbm2ddl.auto">create</property>-->

<mapping resource="hibernateconfigurations/reportColumns.hbm.xml"/>
<mapping resource="hibernateconfigurations/UserRepColsLink.hbm.xml"/>
<mapping resource="hibernateconfigurations/reportDetails.hbm.xml"/>
<mapping resource="hibernateconfigurations/userPreferences.hbm.xml"/>
</session-factory>

</hibernate-configuration>


Thanks in advance
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, Hibernate needs the transactions. Transaction to Session is a one to one relationship.

It waits till the Session is flushed or the transaction is committed before going to the database. Without transactions, Hibernate would have had to hit the database everytime you even just change the value of one attribute. Not very performant if it had to do that.

Mark
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me add one thing. If you were using EJB3 and the container was injecting the JTA transaction into your bean, then no you wouldn't need to have that line of code, but there would still be a transaction.

Mark
 
Darya Akbari
Ranch Hand
Posts: 1855
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Khadija,

if you use Hibernate out of Java EE server, yes you need to start a transaction. But in case you use a Java EE server like JBoss which use Hibernate as it's persistence layer you could write a DAOFactory and get the current session from it, without starting a transation, because the EJB container does it for you.

For more details check out:



Regards,
Darya
 
Khadija Lokhandwala
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You Mark and Darya!!!
Yes Mark, we cannot use Hibernate without Transactions unless we are an Application Server.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic