3

Generally

We have some business logic that is causing a bottle neck within a transaction. The business logic queries the database for a set of data (read only), processes it, and returns an object. This must be done many times with varying parameters in a given request. Can we theoretically break off each business logic call into a separate thread?

Specifically

EJB object (part of an http request on a JBoss App Server)
-creates objects that implement Callable (call method calls business logic method)
-using an ExecutorService invoke each callable object

Business Logic
-Makes a query of postgresql database which uses a PreparedStatement
-Using POJOs we build objects from ResultSet objects that come from postgresql
-Do expensive calculations

After all of this we get postgres errors that unnamed portals don't exist even when we limit our threads to one:

ERROR:  cursor "<unnamed portal 777>" does not exist
STATEMENT:  FETCH ALL IN "<unnamed portal 777>"

I'm not quite sure what is happening to cause the error, but the business logic is being called correctly and it works fine without threading. This leads me to question whether threads can be started and added to a transaction (and if they can how do WE do it?).

Adam
  • 3,675
  • 8
  • 45
  • 77
  • Can you break your transaction up into sub transactions? – Liz Albin Dec 04 '09 at 19:17
  • How are the connections being generated? A connection should be used only in a single thread. – Kathy Van Stone Dec 04 '09 at 19:18
  • @Liz I'm new to transactions in general, so maybe you can add an answer with some info on how sub transactions would fit into this (a thread for each? how does this impact rolling and happens before?) @Kathy I'm not sure what you mean by connection, but the ejb is part of a request to populate a web page and the postgres call uses a PreparedStatement – Adam Dec 04 '09 at 19:25

3 Answers3

3

You should not create threads within an EJB (or anywhere else within an App server for that matter). If you need to break it down, use JMS or even simpler, use WorkManager to provide concurrency within the EJB based operation.

Community
  • 1
  • 1
Robin
  • 24,062
  • 5
  • 49
  • 58
  • Thanks Robin, a handy page that came up for thos in the future was: http://www.ibm.com/developerworks/java/library/j-jca2/ – Adam Dec 04 '09 at 19:47
  • Unfortunately, it looks like Jboss doesn't support the WorkManager interface outside if it's resource management :( – Adam Dec 04 '09 at 20:49
  • @Adam - You can try this - http://www.nexopen.org/confluence/display/NXFWK/CommonJ+and+jBoss+integration – Robin Dec 04 '09 at 20:54
0

In a JDBC context I'd say that if you're able to share the connection on which you've done the "BEGIN TRANSACTION," and to issue all your calls from there, then yes, it should at least theoretically work.

I'm not normally a fan of stored procedures, but have you considered those? If threading within a transaction is possible, I'd expect it to behave a little more sanely within the database engine than dispersed across the boundary.

Carl Smotricz
  • 66,391
  • 18
  • 125
  • 167
0

Though this is probably not the right way to do things this is how to pull this off.

Make the objects that implement Callable not just call the business logic, but do an EJB lookup and call the method. This result looks like this:

EJB object (part of an http request on a JBoss App Server)
-creates objects that implement Callable (call method calls business logic method)
-using an ExecutorService invoke each callable object

Callable Object
-lookup another EJB instance of the class we are already in to get a transaction going
-call that instance's business logic

Business Logic
-Makes a query of postgresql database which uses a PreparedStatement
-Using POJOs we build objects from ResultSet objects that come from postgresql
-Do expensive calculations

I don't much like it which is why I didn't select it as the best answer, but in our specific case it works and I thought it would help others.

Adam
  • 3,675
  • 8
  • 45
  • 77