There's a lot of questions here on this topic, but still I'm not clear with the procedure of managing session within the transaction.
Let's assume there's some repo:
@Repository
public class SomeRepository {
@Autowired
protected SessionFactory sessionFactory;
@Transactional
public void doSomething() throws IOException {
Session session = getSession();
List<SomeEntity> someEntities = session.createCriteria(SomeEntity.class).list();
for (int i = index; i < someEntities.size(); i++) {
/* Some ops with entities */
if (i % 100 == 0) {
session.flush();
session.clear();
}
}
session.close;
}
protected Session getSession() {
return sessionFactory.openSession();
}
}
Is this code correct? Do I really need to open and close (and flush and clear) session manually each time the operation is running? May I use getCurrentSession() instead and forget about flushing and closing (cuz I guess transaction may take care about the lifecycle for me)?