Hibernate version:
Using JBoss 4.0.2 with built in Hibernate
I have built a basic non-EJB struts application.
I deploy my Hibernate config as a HAR file. This works fine.
Then I deploy my Struts application as a WAR file.
I've found that I have to explicitly wrap all my Hibernate activity in a JTA transaction, like the example below, or I get NullPointerException from HibernateContext.getSession(xxx).
Is there a simpler way to do this than declaring a JTA transaction in every struts Action? I realise I could put the data manipulation code into EJBs and let the container manage transactions, but the application isn't big enough to justify that.
Is it possible for the HibernateContext.getSession call to initiate a transaction if one isn't already in progress, and complete the transaction automatically when the servlet request processing finishes?
public ActionForward execute(ActionMapping mapping, ActionForm form, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) {
UserTransaction ut;
ActionForward forward;
try {
InitialContext context = new InitialContext();
ut = (UserTransaction) context.lookup("java:comp/UserTransaction");
ut.begin();
Session session = HibernateContext.getSession("java:/hibernate/LeadsSessionFactory");
|