Forum Discussion

Terkon's avatar
Terkon
Contributor
4 years ago

How to keep database connection open or how to use one connection per testcase

Hi community,   For reasons I want to open a database connection to Oracle manually in groovy, not with the JDBC teststep. And I want to keep this connection open the whole testcase or maybe even ...
  • Terkon's avatar
    Terkon
    4 years ago

    Hi JKolosova ,

     

    thanks for the reply. Actually I was able to keep the connection as described in that link in my first post.

     

    I put up the following script as "setup script" in the testcase level

    //In a Setup Script
    import groovy.sql.Sql
     
    //try to create connection to database, if available. load this connection on context
    //if not, log error and continue
    //In order for this to work, you need to have jdbc driver jar file in $SOAPUI_HOME/bin/ext folder
    def dbString = context.expand( '${#Project#dbString}' )
    def dbDriver = context.expand( '${#Project#dbDriver}' )
    def dbUserName = context.expand( '${#Project#dbUserName}' )
    def dbPassword = context.expand( '${#Project#dbPassword}' )
    
    
    if ( (null != dbString) && (null != dbDriver) && (null != dbUserName) && (null != dbPassword) ) 
    {
      try {
        connection = Sql.newInstance(dbString, dbUserName, dbPassword, dbDriver)
        context.setProperty("dbConn", connection)    
        log.info "Hello DB"
      } catch (Exception e) { 	
      
        log.error "Could not establish connection to the database. " + e.toString()       
      }
    }

     

    The following I set up as a tear down script in the Testcase level

     

    //In a TearDown Script
    //Close db connection
    if (context.dbConn)
    {
      context.dbConn.close()
      log.info "Byebye DB Connection."
    }

     

     

    Then in a groovy teststep I am able to use this context.dbConn until the testcase is finished. Like this:

    The query result are bit strange in format, thats why I put the log.info, so everybody who uses this, will see right away.

     

    if (context.dbConn)
    {
    	try{		
      		log.info "Hello -connection available"
      
      		//connection to the database
      		def sql = context.dbConn
      		  
    		
      		rowSerienNr = sql.firstRow("select nSerienNr from TableSerialNr where product = 'Screen'")  		
      		log.info "QueryResult " + rowSerienNr.toString()
    
    		
    
    	}
    	catch(Exception e)
    	{
    		log.error e.toString()		
    	}
    }
    else {log.error "Error"}

     

    With these scripts I was able to just open 200 DB connections on Oracle instead of serveral 10.000 in a load test using the default JDBC request.

     

    All kudos should go to the guy who posted the info in the link I copied in my first post