0

If I was not using hibernate, I would use jdbcTemplate to make a db call like:

getJdbcTemplate().update("....")

I did this in the past, I didn't write any xml bean configuration or use any annotations for Transactions. So what kind of transaction scope did I have then?

Currently when using spring mvc with hibernate, i'm injecting the sessionManager (not using HibernateDaoSupport) and I put the @Transaction annotation on my service class (which uses the Dao).

@Service
@Transactional
public class UserServiceImpl extends UserService {

   @Autowired
   UserService userService

   @Override
   public User getUser(int id) {
       return userDao.get(User.class, id);
   }

}

My application context XML has:

<context:component-scan base-package="com.myapp"/>
<mvc:annotation-driven/>

<bean id="dataSource" .../>
<bean id="sessionFactory" .../>

<tx:annotation-driven />

At this point I really don't care about transactions spanning more than a single db call, and I want things to be as fast as possible.

How can I do exactly what the JdbcTemplate does?

What are the various options when it comes to transactions, specifically looking for ways to minimize table/row locking as much as possible (I'm guessing what I want is what jdbcTemplate did for me out-of-the-box).

andrew cooke
  • 45,717
  • 10
  • 93
  • 143
Blankman
  • 259,732
  • 324
  • 769
  • 1,199

2 Answers2

1

When your are using @Transactional without specifying any isolation level, it will default to default isolation level of the underlying datastore. It will be true for jdbc as well as hibernate.

If you want to change isolation level you will have to provide isolation enum in @Transactional.

@Transactional(isolation=Isolation.???)

Where ??? can be level as described here .

  1. READ_COMMITTED
  2. READ_UNCOMMITTED
  3. REPEATABLE_READ
  4. SERIALIZABLE
  5. DEFAULT

To set an isolation level other than default you will have to set a boolean property "allowCustomIsolationLevels" of transaction manager as true if using JTA transaction.

You can refer more on settings in spring docs for @Transactional

baba.kabira
  • 3,111
  • 2
  • 26
  • 37
0

About "as fast as possible", I made a reply to this kind of question using stateless session:

Best way to insert a good amount of records in hibernate

Basically (I copy/paste my code from my answer in the above question):

Session session = sessionFactory.openStatelessSession();
Transaction tx = session.beginTransaction();
for ( int i=0; i<100000; i++ ) {
    Item item = new Item(...);
    session.save(item);
    if ( i % 100 == 0 ) {
        session.flush();
        session.clear(); // not needed ? (see below)
    }
}
tx.commit();
session.close();
Community
  • 1
  • 1
Kartoch
  • 7,610
  • 9
  • 40
  • 68