• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JPA, EntityTransation: Too many connections etc

 
Greenhorn
Posts: 27
IntelliJ IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have decided to test my server by making a Bot class which I instantiate by hundreds and use to simulate clients.
That forced me to create hundreds of accounts in the database... And it shows the way I manage the database is flawed.
I have tried to create the bot accounts in two different ways, each of them leads to a different issue.


1) This one crashes while trying to create second bot and procudes the following exception:
Exception in thread "main" java.lang.IllegalStateException:
Exception Description: Transaction is currently active


Here's my explanation about case 1: the commit on the EntityTransation doesn't release it sufficently quick. I don't understand why =( All the creation happens on a signle java Thread, in sequence.

2) Let's add a small sleep. This time it doesn't crash on the second bot but on the 45th bot (always on the 45th):
Data source rejected establishment of connection, message from server: "Too many connections"


I don't understand this case.
I'm using eclipselink.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the first case, how are you firing off these bots?
What connection are they using with your server?
Are they sharing a connection?
(the connection in the above is the connection from your test bot stuff to the server, not the db connection)

For the second, you are not closing your db-connections, so you are flooding the db server.
 
Jean-Michel Vilain
Greenhorn
Posts: 27
IntelliJ IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My problem only concerns the bot accounts creation in the database, and it is ran by my server when I type 'resetDb'. So in the examples above, the bots don't connect to anything, it's just the main server thread trying to create them in a fast loop.

About the 'too many connections', yes I'm probably not closing every connection.
I will investigate that, but I'd like to understand why trying to create the bots too fast (case 1) leads to a 'Transaction is currently active'.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jean-Michel Vilain wrote:
I will investigate that, but I'd like to understand why trying to create the bots too fast (case 1) leads to a 'Transaction is currently active'.



Ah, OK.
Misunderstood.
I'd need to see what the createBotAccount code actually does and where you are opening and closing transactions.

Actually, why not simply create all the accounts in a single transaction?
 
Jean-Michel Vilain
Greenhorn
Posts: 27
IntelliJ IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The account creation is a separate function which does many stuff (as shown below), I don't want to write a custom method for creating X bot accounts. Of course it's not optimized but that's not the goal here, the bot accounts creation happens once and for testing purpose.

I have fixed case 2 (the 'Too many connections', indeed I left connections open).
But case 1 remains a mystery! I don't want to leave a sleep(100) there. I want to understand

The bot account creation happens via 2 distinct methods:

First, ActivationKeyDAO.create is called to generate a valid activation key.
Second, AccoountDAO.createAndActivate is called to create the account and mark the key as used.

Here is ActivationKeyDAO.create:



And here is AccountDAO.createAndActivate:






 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's the full stack trace you get for that IllegalStateException?

(I can well understand not wanting to leave a pause like that in there...)
 
Jean-Michel Vilain
Greenhorn
Posts: 27
IntelliJ IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exception in thread "main" java.lang.IllegalStateException:
Exception Description: Transaction is currently active
at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.begin(EntityTransactionImpl.java:89)
at faeria.world.ActivationKeyDAO.create(ActivationKeyDAO.java:61)
at faeria.world.FaeriaWorld.createBotAccount(FaeriaWorld.java:704)
at faeria.world.FaeriaWorld.createBotAccounts(FaeriaWorld.java:641)
at faeria.world.FaeriaWorld.createDatabase(FaeriaWorld.java:563)
at faeria.world.ConsoleCommands$ResetDatabaseCommand.exec(ConsoleCommands.java:91)
at faeria.world.Command.exec(CommandRunner.java:80)
at faeria.world.CommandRunner.processCommand(CommandRunner.java:45)
at faeria.world.FaeriaWorld.main(FaeriaWorld.java:361)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

That means it's the line
locEntityTransaction.begin();
inside ActivationKeyDAO who crashes. Probably because the last call to Account.create didn't release that Transaction fast enough. The two DAOs are using the EntityTransaction in turn, here a sample sequence:
Account.create
ActivationKeyDAO.createAndActivate << crashes
Account.create
ActivationKeyDAO.createAndActivate
Account.create
ActivationKeyDAO.createAndActivate
...
 
Dave Tolls
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to the stack trace it's the create that fails. The first bit it does for each account (which makes some sense since the pause "fixes" it).

I would look at the second bunch of stuff in the createAndActivate,
You're mixing JPA with what looks like straight JDBC, and then persisting the new parActivationKey, which I don't see a new transaction there?
Or a subsequent commit?

That's the area I'd be investigating as it's not the same structure as the rest of it (ie, begin...do JPA stuff...commit/rollback).

Sorry it's only a brief look...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic