Recently I started working with isolation levels, but specifying isolation level in @Transactional
annotation does not seems to work in spring boot. I tried a lot of different things but I cannot get it to work, below is my code.
@Transactional(isolation=Isolation.READ_UNCOMMITTED)
public void updateWalletAddresses(List<RegisterWallet> walletMetaCollection) throws Exception{
Transaction tx =null;
Session session =null;
if(sessionFactory == null){
return;
}
session = sessionFactory.openSession();
try{
String sql = "select * from user";
SQLQuery query = session.createSQLQuery(sql);
query.addEntity(User.class);
List<User> userlist=query.list();
int i=0;
while(i<userlist.size()){
System.out.println(userlist.get(i).getEmail());
i++;
}
}catch(Exception e){
throw e;
}
}
before executing the above walletservice method I am starting a transaction in mysql client but I am not committing it so that I have dirty data. After that I executed above code but it does not print uncommitted data even though I specified transaction read uncommitted. code for starting transaction in mysql is
set autocommit=0;
start transaction;
insert into user (name,email,password,roleid,username)
values("prashank","[email protected]","password",1,"prashank");
Note: as I am not committing the transaction then above insert cause dirty read problem. I am not able to read uncommitted data Note: Similarly any other isolation level are not working . Please help
the mere presence of the @Transactional annotation is not enough to activate the transactional behavior. The @Transactional annotation is simply metadata that can be consumed by some runtime infrastructure that is @Transactional-aware and that can use the metadata to configure the appropriate beans with transactional behavior.
– stephen.vakilopenSession
but usecurrentSession
instead. When opening a new session you might even get a new transaction, rendering the@Transactional
useless. (WithopenSession
you are opening a new session outside the scope of spring, you aren't closing it so you are also leaking connections). – M. Deinum