2

I'm using JPA/Eclipselink in Java SE (Oracle Java 7) and Java EE (Websphere Liberty) environments.

I have a component which is called from both Java SE and Java EE (by component I mean the same source code), which executes JPA queries. (the Java SE run is started with -javaagent:lib/eclipselink.jar param)

I have conducted 3 test runs:

  1. in Java EE - the code runs fast (e.g. queries take around 0.01 seconds)
  2. in Java SE - the code runs slow (2 seconds/query)
  3. in Java SE AND Java EE server is running - the code runs as fast as in the first case

I'm a bit puzzled; why does starting up a Java EE server causes code (running in a different JVM) to speed up? And what can I do to speed up the queries in Java SE without running a Java EE server?

UPDATE

This issue might be related to the underlying database. I run the applications in the following order

  1. started Java SE app - queries are slow
  2. started Java EE app - queries in Java SE app are fast
  3. stopped Java EE app - queries are still fast in Java SE app

So, there might be some kind of database cache(?) which the Java EE app initializes, but the Java SE app doesn't - however I don't see anything in the log what might be related to this.

One more thins I saw, is that the two apps use a different database driver (which might cause some performance difference, but wouldn't explain why starting both apps would cause the speed up ...)

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
krisy
  • 1,508
  • 1
  • 14
  • 32
  • thought of looking at the log, and what is in the CLASSPATH? – Neil Stockton Mar 12 '15 at 07:57
  • By the classpath, you mean the classparth of the J2SE app? The two apps are basically two different project (in Eclipse), and the J2SE project references to the JEE project (which contains the source code of the component) – krisy Mar 12 '15 at 08:01
  • The logs tells ... well, a lot of stuff :-) What should I look for? I see messages like "The column name for element ...", "Detected database platform ...", and connection information, etc. – krisy Mar 12 '15 at 08:12
  • no idea of the content of EclipseLink logs (since I dont use it), but you are comparing queries, so look for those queries and timings – Neil Stockton Mar 12 '15 at 08:14
  • Did you conduct the tests on the same machine? On the same Java runtime? Running under the same user? With the same network environment? (proxy, firewall, etc.) – Gimby Mar 12 '15 at 08:44
  • Gimby: yes, everythin is the same – krisy Mar 12 '15 at 08:45
  • Neil: the funny thing is that the queries are the same - but take different time to execute – krisy Mar 12 '15 at 08:45
  • You would have to show what you are doing in the tests to determine what might be different, as there is a lot of lifecycle management that can really speedup/slow things down that is handled by the container for you. For instance, if you are closing the factory between each query etc, there are many things that might cause differences between your environments/tests. Try running with a performance profiler and seeing what is taking more time. – Chris Mar 12 '15 at 15:00
  • There are no differences in the code - but just out of curiosity, how would that relate to that with running the J2SE code with JEE running in the background runs fast, but without JEE running in the background is slow? – krisy Mar 12 '15 at 15:13
  • So the exact same JPA properties and datasource config is used in both environments? How you are managing the EntityManagerFactory/EntityManager instances will definitely affect performance differently, though I can not say why running in a different VM with a server active would change performance unless something in your database is caching queries and their results. profiling will tell you what it taking the time, such as always closing the factory in a J2SE environment might cause it to constantly release the PU and reload it for the next run, while it remains cached in JEE – Chris Mar 18 '15 at 16:29
  • "in Java SE - the code runs slow (2 seconds/query)" does that include the time to open database connection and instantiate an entity manager factory / entity manager, or just the time for the actual query? – Nicholas Hirras Mar 18 '15 at 21:39
  • Nicholas: just the queries - but I'm really puzzled, since those queries are fetching rows by primary key, so there could be another (time?) issue there ... also updated the question – krisy Mar 23 '15 at 08:51

1 Answers1

2

Finally, I figured it out:

  • in JEE enviroment JPA used connection pooling automatically; but in JSE enviroment, no connection pooling was available
  • when a connection to any db2 database created, a number of agents are started (e.g. monitoring, collection statistics, etc.); to start (and terminate) these agents it took about 2 seconds (these agents were visible in db2 get snapshot on all for database output)
  • when JEE was running, a persistent (pooled) connection was maintained to the db2 scema; this way all agents were running
  • when the J2SE app was started with the agents running, the overhead of creating a database connection was very small; on the other hand if no persistent connection was available this overhead was small

Got the queries (connections) speed up by using C3PO.

krisy
  • 1,508
  • 1
  • 14
  • 32