title: How to trace and prevent the deadlock appeared in c3po which is running in seperate processes?
body: <p>I have a very simple computation which produces letter matrices finds probably all the words in the matrix. The letters in the word are adjacent cells. </p>
  
  <pre><code>   for (int i = 0; i &lt; 500; i++) {
          System.out.println(i);
          Matrix matrix = new Matrix(4);
          matrix.scanWordsRandomly(9);
          matrix.printMatrix();
          System.out.println(matrix.getSollSize());
          matrix.write_to_db();
      }
  </code></pre>
  
  <p>Here is the persisting code.</p>
  
  <pre><code>public void write_to_db() {
      Session session = null;
      try {
          session = HibernateUtil.getSessionFactory().openSession();
          session.beginTransaction();
          Matrixtr onematrixtr = new Matrixtr();
          onematrixtr.setDimension(dimension);
          onematrixtr.setMatrixstr(this.toString());
          onematrixtr.setSolsize(getSollSize());
          session.save(onematrixtr);
          for (Map.Entry&lt;Kelimetr, List&lt;Cell&gt;&gt; sollution : sollutions.entrySet()) {
              Kelimetr kelimetr = sollution.getKey();
              List&lt;Cell&gt; solpath = sollution.getValue();
              Solstr onesol = new Solstr();
              onesol.setKelimetr(kelimetr);
              onesol.setMatrixtr(onematrixtr);
              onesol.setSoltext(solpath.toString().replace("[", "").replace("]", "").replace("true", "").replace("false", ""));
              session.save(onesol);
          }
  
          session.getTransaction().commit();
          session.close();
  
      }
      catch (HibernateException he) {
          System.out.println("DB Error : " + he.getMessage());
          session.close();
      } 
      catch (Exception ex) {
          System.out.println("General Error : " + ex.getMessage());
      }
  }
  </code></pre>
  
  <p>Here is the hibernate configuration file.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
  &lt;hibernate-configuration&gt;
    &lt;session-factory&gt;
      &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;
      &lt;property name="hibernate.connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
      &lt;property name="hibernate.connection.url"&gt;jdbc:mysql://localhost:3306/kelimegame_db_dev?autoReconnect=true&amp;amp;useUnicode=true&amp;amp;characterEncoding=UTF-8&lt;/property&gt;
      &lt;property name="hibernate.connection.username"&gt;root&lt;/property&gt;
      &lt;property name="hibernate.connection.password"&gt;!.Wlu9RrCA&lt;/property&gt;
      &lt;property name="hibernate.show_sql"&gt;false&lt;/property&gt;
      &lt;property name="hibernate.query.factory_class"&gt;org.hibernate.hql.classic.ClassicQueryTranslatorFactory&lt;/property&gt;
      &lt;property name="hibernate.format_sql"&gt;false&lt;/property&gt;
      &lt;!-- Use the C3P0 connection pool provider --&gt;
      &lt;property name="hibernate.c3p0.acquire_increment"&gt;50&lt;/property&gt;
      &lt;property name="hibernate.c3p0.min_size"&gt;10&lt;/property&gt;
      &lt;property name="hibernate.c3p0.max_size"&gt;100&lt;/property&gt;
      &lt;property name="hibernate.c3p0.timeout"&gt;300&lt;/property&gt;
      &lt;property name="hibernate.c3p0.max_statements"&gt;5&lt;/property&gt;
      &lt;property name="hibernate.c3p0.idle_test_period"&gt;3000&lt;/property&gt;
      &lt;mapping resource="kelimegame/entity/Progress.hbm.xml"/&gt;
      &lt;mapping resource="kelimegame/entity/Solstr.hbm.xml"/&gt;
      &lt;mapping resource="kelimegame/entity/Kelimetr.hbm.xml"/&gt;
      &lt;mapping resource="kelimegame/entity/User.hbm.xml"/&gt;
      &lt;mapping resource="kelimegame/entity/Achievement.hbm.xml"/&gt;
      &lt;mapping resource="kelimegame/entity/Matrixtr.hbm.xml"/&gt;
    &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>After finding all possible solutions I persist the matrix and the solutions using hibernate. I am also using c3pO library. I am not spawning any thread. All the work is being done in a very simple iterative way. But I am running the jar in separate processes. 
  From different terminals I am executing this : </p>
  
  <pre><code>java -jar NewDB.jar
  </code></pre>
  
  <p>I got a deadlock as follows :</p>
  
  <pre><code>Apr 25, 2013 8:38:05 PM com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector run
  WARNING: com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@7f0c09f9 -- APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks!
  Apr 25, 2013 9:08:23 PM com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector run
  WARNING: com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@7f0c09f9 -- APPARENT DEADLOCK!!! Complete Status: 
      Managed Threads: 3
      Active Threads: 3
      Active Tasks: 
          com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask@2933f261
              on thread: C3P0PooledConnectionPoolManager[identityToken-&gt;z8kfsx8uibeyqevbbapc|4045cf35]-HelperThread-#1
          com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask@116dd369
              on thread: C3P0PooledConnectionPoolManager[identityToken-&gt;z8kfsx8uibeyqevbbapc|4045cf35]-HelperThread-#0
          com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask@41529b6f
              on thread: C3P0PooledConnectionPoolManager[identityToken-&gt;z8kfsx8uibeyqevbbapc|4045cf35]-HelperThread-#2
      Pending Tasks: 
          com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@165ab5ea
          com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@1d5d211d
          com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@4d2905fa
  Pool thread stack traces:
      Thread[C3P0PooledConnectionPoolManager[identityToken-&gt;z8kfsx8uibeyqevbbapc|4045cf35]-HelperThread-#1,5,main]
          com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:662)
      Thread[C3P0PooledConnectionPoolManager[identityToken-&gt;z8kfsx8uibeyqevbbapc|4045cf35]-HelperThread-#0,5,main]
          com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:662)
      Thread[C3P0PooledConnectionPoolManager[identityToken-&gt;z8kfsx8uibeyqevbbapc|4045cf35]-HelperThread-#2,5,main]
          com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:662)
  
  
  Apr 25, 2013 9:41:29 PM com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector run
  WARNING: com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@7f0c09f9 -- APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks!
  Apr 25, 2013 9:55:18 PM com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector run
  WARNING: com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@7f0c09f9 -- APPARENT DEADLOCK!!! Complete Status: 
      Managed Threads: 3
      Active Threads: 3
      Active Tasks: 
          com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@5a337b7d
              on thread: C3P0PooledConnectionPoolManager[identityToken-&gt;z8kfsx8uibeyqevbbapc|4045cf35]-HelperThread-#0
          com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@69f079ce
              on thread: C3P0PooledConnectionPoolManager[identityToken-&gt;z8kfsx8uibeyqevbbapc|4045cf35]-HelperThread-#1
          com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@2accf9b8
              on thread: C3P0PooledConnectionPoolManager[identityToken-&gt;z8kfsx8uibeyqevbbapc|4045cf35]-HelperThread-#2
      Pending Tasks: 
          com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask@771eb4fb
          com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@fc07d6
          com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask@2266731b
          com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@740f0341
          com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask@59edbee
          com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@78e924
          com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask@2123aba
          com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@7acd8a65
  Pool thread stack traces:
      Thread[C3P0PooledConnectionPoolManager[identityToken-&gt;z8kfsx8uibeyqevbbapc|4045cf35]-HelperThread-#0,5,main]
          java.text.NumberFormat.getInstance(NumberFormat.java:769)
          java.text.NumberFormat.getInstance(NumberFormat.java:393)
          java.text.MessageFormat.subformat(MessageFormat.java:1262)
          java.text.MessageFormat.format(MessageFormat.java:860)
          java.text.Format.format(Format.java:157)
          java.text.MessageFormat.format(MessageFormat.java:836)
          com.mysql.jdbc.Messages.getString(Messages.java:106)
          com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2552)
          com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3002)
          com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2991)
          com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3532)
          com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:943)
          com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4113)
          com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1308)
          com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2336)
          com.mysql.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2176)
          com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2158)
          com.mysql.jdbc.ConnectionImpl.&lt;init&gt;(ConnectionImpl.java:792)
          com.mysql.jdbc.JDBC4Connection.&lt;init&gt;(JDBC4Connection.java:47)
          sun.reflect.GeneratedConstructorAccessor7.newInstance(Unknown Source)
          sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
          java.lang.reflect.Constructor.newInstance(Constructor.java:525)
          com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
          com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)
          com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
          com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:134)
          com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:183)
          com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:172)
          com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:188)Killed
  caglar@ubuntu:~/NetBeansProjects/NewDB/dist$ 
  </code></pre>
  
  <p>My question is as follows :</p>
  
  <ol>
  <li>Can this deadlock in c3po happen since I am running the program in
  separate processes? </li>
  <li>Should I use one process and multiple threads inside of this
  process?</li>
  <li>How can I trace this deadlock understand the cause of it? Is there a way to trace multiple JVMs causing deadlocks?</li>
  </ol>