Good old connection pool using Agents

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Good old connection pool using Agents

Vinay Samudre
This post has NOT been accepted by the mailing list yet.
Hi All,

Trying a connection pool using Agents

Environment:
Using Groovy Version: 1.7.1 JVM: 1.5.0_12
OS: Solaris

Here is my take based on my limited knowledge of gpars:

@Grab(group='org.codehaus.gpars', module='gpars', version='0.10')
import groovyx.gpars.agent.Agent
import groovyx.gpars.GParsExecutorsPool
import groovy.sql.Sql
import java.sql.Connection

public class ConnectionPool {
   private def pool = new Agent([])
   
   public void init() {
      2.times { i ->
         println "init called...."
         def dbHandle = null
         dbHandle = Sql.newInstance("jdbc:informix-sqli://brumby3:6010/VUSISv3:INFORMIXSERVER=brumby_idm_n;ifx_autofree=true;OPTOFC=1;IFX_LOCK_MODE_WAIT=30;", "samudrv", "VinU100", "com.informix.jdbc.IfxDriver")
         println "creating myConnection"
         MyConnection myc = new MyConnection(name : "conn-${i+1}",
                     takenBy : null, connection : dbHandle, takenCount: 0)
         println "adding ${myc.dump()} to pool"
         pool << { it << myc }
      }
   }

   def getConnection(threadName) {
     def c = pool << { p ->
                     def c1 = p.find { it != null && it.takenBy == null }
                     if ( null !=  c1 ) {
                        println "Found Connection ${c1}"
                        c1.takenBy = threadName
                        c1.takenCount++
                        println "getConnection Called by ${c1.takenBy} giving ${c1.name} tc=${c.takenCount}"
                        return c1
                     } else {
                        println "getConnection Called by ${threadName} no available connections"
                        return null
                     }
             }
   }

   public void releaseConnection(conn) {
     if ( null == conn) { return }
     if ( conn.takenCount != 1 ) {
        println "===>>>releaseConnection Called by ${conn.takenBy} releasing ${conn.name} but takencount is not 1? ${conn.takenCount}"
        println ">>>>>>>>>>>>>>>>"
     }
     conn.takenCount--
     println "releaseConnection Called by ${conn.takenBy} releasing ${conn.name} tc=${conn.takenCount}"
     conn.takenBy = null
   }

   public void closeAll() {
     pool << { it.each { it.connection.close() } }
   }
   
   public void report() {
      pool << { println "Report=> ${it}" }
   }
}
public class MyConnection {
 String name
 String takenBy
 Sql connection
 int takenCount = 0
 public String toString() { "MyConnection[$name-$takenBy]" }
}


public static void main(args) {
   def cp1 = new ConnectionPool();
   cp1.init()
   cp1.report()
   sleep 1000


   def myTask = {
      def threadName = Thread.currentThread().name
      println "Starting Thread ${threadName}"
      println "${threadName} calling get"
      def c
      c = cp1.getConnection(threadName)
      println "c=${c.dump()}"
      if ( null != c ) {
         println "${threadName} calling release"
         cp1.releaseConnection(c)
      } else {
         println "${threadName} could not get connection"
      }
      println "Finishing Thread ${threadName}"
   }

   GParsExecutorsPool.withPool {
      myTask.callAsync()
   }
}

And here is output.....
<OUTPUT>
init called....
creating myConnection
adding <MyConnection@97d3f0 name=conn-1 takenBy=null connection=groovy.sql.Sql@6
50646 takenCount=0> to pool
init called....
creating myConnection
adding <MyConnection@1a734ff name=conn-2 takenBy=null connection=groovy.sql.Sql@
886ad0 takenCount=0> to pool
Report=> [MyConnection[conn-1-null], MyConnection[conn-2-null]]
Starting Thread Thread-1
Thread-1 calling get
Found Connection MyConnection[conn-1-null]
</OUTPUT>

Is my approch using agent is correct?
As you can see println "getConnection Called by ${c1.takenBy} giving ${c1.name} tc=${c.takenCount}" is not being executed. Is this a bug or my approch is wrong?

Please help me understand gpars better.

cheers
Vinay