-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 posts ] 
Author Message
 Post subject: Urgent!Transaction not successfully started!
PostPosted: Mon Dec 04, 2006 8:06 pm 
Newbie

Joined: Mon Dec 04, 2006 9:30 am
Posts: 5
Hibernate version:3.1
Database:SQL Server 2005 with SP1
WebServer:Tomcat 5.5.20
IDE:Eclipse 3.2


I can not use transaction!!!

Code between sessionFactory.openSession() and session.close():

try {
session = HSessionFactory.getSession();
tx = session.beginTransaction();
Query query = session
.createQuery("from Users as u where u.name = '" + userName
+ "' and u.pwd='" + pwd + "'");
if (query != null) {
List list = query.list();
if (list.size() == 1) {
result = true;
} else {
result = false;
}
}
tx.commit();
return result;
} catch (HibernateException e) {
throw e;
} finally {
if (tx != null) {
tx.rollback();
}
HSessionFactory.closeSession();
}
Full stack trace of any exception that occurs:
type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Transaction not successfully started
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:523)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root cause

org.hibernate.TransactionException: Transaction not successfully started
org.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:149)
com.lst.webshop.user.model.UsersService.validateUser(UsersService.java:37)
com.lst.webshop.user.action.UserLoginAction.execute(UserLoginAction.java:46)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


note The full stack trace of the root cause is available in the Apache Tomcat/5.5.20 logs.


--------------------------------------------------------------------------------


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 05, 2006 2:24 am 
Newbie

Joined: Sat Dec 02, 2006 4:23 am
Posts: 17
I think the following code may help
Code:
SessionFactory.getCurrentSession().beginTransaction();

U can begin the transaction on the current session only.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 05, 2006 6:08 am 
Beginner
Beginner

Joined: Tue Nov 21, 2006 5:18 am
Posts: 31
Location: Bangalore, India
Hi,

The trx.commit() or trx.rollback() is valid whenever you do any session.save or update operation.

its not necessary for select operation, the exception is caused because without doing any commit(there is nothing to commit for select operation), its trying to do rollback.

_________________
persist_coder
--credit please if it helps you


Top
 Profile  
 
 Post subject: Thank a lot.But problem still can't be solved.
PostPosted: Tue Dec 05, 2006 8:54 am 
Newbie

Joined: Mon Dec 04, 2006 9:30 am
Posts: 5
Anyway thx a lot


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 05, 2006 10:17 am 
Beginner
Beginner

Joined: Tue Nov 21, 2006 5:18 am
Posts: 31
Location: Bangalore, India
can you post your code, what you have done after this fix. I can help you

_________________
persist_coder
--credit please if it helps you


Top
 Profile  
 
 Post subject: my code
PostPosted: Tue Dec 05, 2006 10:40 am 
Newbie

Joined: Mon Dec 04, 2006 9:30 am
Posts: 5
public void create(Users user) {
Session session = null;
Transaction tx = null;
try {
session = HSessionFactory.getSession();
tx = session.getSessionFactory().getCurrentSession().beginTransaction();
session.save(user);
tx.commit();
} catch (HibernateException e) {
throw e;
} finally {
if (tx != null) {
tx.rollback();
}
HSessionFactory.closeSession();
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 05, 2006 11:04 am 
Beginner
Beginner

Joined: Tue Nov 21, 2006 5:18 am
Posts: 31
Location: Bangalore, India
Hi,

Please find the below,

public void create(Users user) {
Session session = null;
Transaction tx = null;
try {
session = HSessionFactory.getSession();
sessionexec = session.getSessionFactory().getCurrentSession();
tx = sessionexec.beginTransaction();
sessionexec.save(user);
tx.commit();
} catch (HibernateException e) {
tx.rollback();
throw e;
} finally {
HSessionFactory.closeSession();
}
}

If the problem still exists, it may be because of HSessionFactory

_________________
persist_coder
--credit please if it helps you


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 05, 2006 11:06 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
The error was in your finally block
Code:
if (tx != null) {
tx.rollback();
}


The Transaction object is not null after you commit. If you want to use the same code, just modify to this:
Code:
if (tx != null && tx.isActive()) {
tx.rollback();
}


Top
 Profile  
 
 Post subject: I beat it:)
PostPosted: Tue Dec 05, 2006 12:02 pm 
Newbie

Joined: Mon Dec 04, 2006 9:30 am
Posts: 5
Thx for Ananasi,persist_coder,jasmine.Especially thx for persist_coder.
Thx for your helps.
I fix the problem by myself now.

I modified my method create():
==========================
public void create(Users user) {
Session session = null;
Transaction tx = null;
try {
session = HSessionFactory.getSession();
HSessionFactory.beginTransaction();
session.save(user);
HSessionFactory.commitTransaction();
} catch (HibernateException e) {
throw e;
} finally {
if (tx != null) {
tx.rollback();
}
HSessionFactory.closeSession();
}
}
==========================


And modified my class HSessionFactory also
==========================
...
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;
private static final ThreadLocal<Transaction> tLocaltx = new ThreadLocal<Transaction>();
...
public static void beginTransaction() {
Transaction tx = (Transaction) tLocaltx.get();
try{
if (tx == null) {
tx = getSession().beginTransaction();
tLocaltx.set(tx);
}
} catch (HibernateException e) {
// throw new InfrastructureException(e);
}
}
public static void commitTransaction() {
Transaction tx = (Transaction) tLocaltx.get();
try {
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack())
tx.commit();
tLocaltx.set(null);
} catch (HibernateException e) {
// throw new InfrastructureException(e);
}
}
public static void rollbackTransaction() {
Transaction tx = (Transaction) tLocaltx.get();
try {
tLocaltx.set(null);
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
tx.rollback();
}
} catch (HibernateException e) {
// throw new InfrastructureException(e);
}
}
==========================


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 05, 2006 12:06 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Heh, now your finally block will never execute (tx == null always).


Top
 Profile  
 
 Post subject: thx again
PostPosted: Wed Dec 06, 2006 7:35 am 
Newbie

Joined: Mon Dec 04, 2006 9:30 am
Posts: 5
Oops, thx for reminding:)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.