I've been running a decently sized app using the PB api in testing for
awhile and recently started getting seemingly random
TransactionInProgress and TransactionNotInProgress exceptions. It
originally looked like the error was a "I don't know how to increment
blah" but further research shows that the problem first occurs in the
SequenceManagerHiLoImpl class when it tries to go to the database and
get the next sequence. Once this error happens it seems to hose up the
connections in the broker pool, halting further database access. I'm
running 0.9.7 with SQL sever 2000. I get the same problem on 2 different
linux computers as well. I changed the grabsize to 1 instead of 10 to
try to bring out the problem since it's hard to recreate.
The following code -sometimes- creates this error (:
public static PersistenceBroker getBroker() {
return PersistenceBrokerFactory.defaultPersistenceBroker();
}
public static Entity_roles createRoleForUser(int userId, int roleId)
throws Exception {
PersistenceBroker broker = getBroker();
// now perform persistence operations
try {
// 1. open transaction
broker.beginTransaction();
if (role.getCreated_date() == null) {
Timestamp currentTime = new
Timestamp(System.currentTimeMillis());
role.setCreated_by("VF_REG");
role.setCreated_date(currentTime);
}
// 2. make the new object persistent
broker.store(role);
if (broker.isInTransaction()) { // stuck this in to try to
get around the problem, it doesn't help though
broker.commitTransaction();
}
} catch (Exception e) {
// if something went wrong: rollback
log.error( "Error createrole()", e);
broker.abortTransaction();
} finally {
if (broker!=null) {
broker.close();
}
}
return role;
}
org.apache.ojb.broker.TransactionInProgressException
at
org.apache.ojb.broker.singlevm.PersistenceBrokerImpl.beginTransaction(Un
known Source)
at
org.apache.ojb.broker.util.sequence.SequenceGenerator.getNextSequence(Un
known Source)
at
org.apache.ojb.broker.util.sequence.SequenceManagerHiLoImpl.getUniqueId(
Unknown Source)
at
org.apache.ojb.broker.singlevm.PersistenceBrokerImpl.getUniqueId(Unknown
Source)
at
org.apache.ojb.broker.metadata.ClassDescriptor.getAutoIncrementValue(Unk
nown Source)
at
org.apache.ojb.broker.metadata.ClassDescriptor.getKeyValues(Unknown
Source)
at org.apache.ojb.broker.Identity.<init>(Unknown Source)
at
org.apache.ojb.broker.singlevm.PersistenceBrokerImpl.store(Unknown
Source)
at
org.apache.ojb.broker.singlevm.PersistenceBrokerImpl.store(Unknown
Source)
at
com.metrowerks.mdp2.user.UserManager.createRoleForUser(UserManager.java:
894)
at
com.metrowerks.mdp2.user.UserManager.updateUserRoles(UserManager.java:11
15)
at
com.metrowerks.vodafone2.action.UserRolesAction.doAction(UserRolesAction
.java:48)
at
com.metrowerks.mdp2.action.LoginReqAction.perform(LoginReqAction.java:38
)
at
org.apache.struts.action.ActionServlet.processActionPerform(ActionServle
t.java:1787)
at
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1586)
at
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:510)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:165)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:103)
at
com.caucho.server.http.FilterChainServlet.doFilter(FilterChainServlet.ja
va:96)
at
com.caucho.server.http.Invocation.service(Invocation.java:312)
at
com.caucho.server.http.CacheInvocation.service(CacheInvocation.java:135)
at
com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:244)
at
com.caucho.server.http.HttpRequest.handleConnection(HttpRequest.java:163
)
at com.caucho.server.TcpConnection.run(TcpConnection.java:137)
at java.lang.Thread.run(Thread.java:479)
So basically what I don't understand about OJB is that since it doesn't
support nested transactions, how am I supposed to use a transaction to
save a new record? If I do that, then when the sequence manager tries to
get a new sequence(and thus starting a transaction), it's already in a
transaction. So how does this ever work?
Do I have to call new Identity(object) before I start my own
broker.beginTransaction() call? Is there an error in the way I'm using
transactions with the persistence broker?
Ryan