Открытая сессия Hibernate: транзакция на запрос?

Я использую Hibernate с Spring на Tomcat. Я читал и перечитывал часто указываемыеJBoss вики-страница по теме, и это было полезно. Но это оставляет меня с некоторыми вопросами.

The idea of starting a transaction for every request troubles me. I guess I could limit the filter to certain controllers -- maybe put all my controllers that need a transaction under a pseudo "tx" path or something. But isn't it a bad idea to use transactions if you don't know if you're going to need one? And if I'm just doing reads in some request -- reads that very likely may come from a cache -- aren't I better off without a transaction?

I've read posts mentioning how they handled the transactions at the service layer, and I'd like to do this with Spring. But then what does the filter code look like? I still want the session available in my view for some lazy loading.

If all I have to do is call sessionFactory.getCurrentSession() in my filter, how does it get "freed" back to the session factory for re-use? (I expected to see a session.close() or something, even when using transactions.) Who is telling the session factory that that session can be reused?

Perhaps it's the beginTransaction() call that binds a given database connection to a given session for the duration of a request? Otherwise, a session pulls db connections from the pool as needed, right?

Спасибо за ваше терпение со всеми моими вопросами.

(И если вашим ответом будет ссылка на документацию Spring, вы просто заставите меня плакать. Вы этого не хотите, не так ли? Я заплачу реальные деньги, если люди перестанут отвечать на вопросы, связанные со Spring, которые путь.)

Ответы на вопрос(2)

Ваш ответ на вопрос