var sql = require('sql-ringojs-client');

// DO NOT TOUCH THE FOLLOWING LINE.
// THIS VARIABLE IS REGEX REPLACED BY setup.py
var dbHost = 'localhost';

exports.app = function(req) {
   var path = req.pathInfo;
   if (path === '/json') {
      var helloObject = {message: "Hello, world"};
      // JSON Response Test
      return {
         status: 200,
         headers: {"Content-Type": "application/json; charset=UTF-8"},
         body: [JSON.stringify(helloObject)]
      }
   } else if (path === '/db') {
      var queryCount = req.env.servletRequest.getParameter('queries');
      var connection = datasource.getConnection();
      if (queryCount === null) {
         var randId = ((Math.random() * 10000) | 0) + 1
         var world = sql.query(connection, 'select * from World where World.id = ' + randId)[0];
         // let's just hope it gets to this
         connection.close();
         return {
            status: 200,
            headers: {"Content-Type": "application/json; charset=UTF-8"},
            body: [JSON.stringify(world)]
         }
      } else {
         queryCount = parseInt(queryCount, 10);
         var body = [];
         var randId, world;
         for (var i = 0; i < queryCount; i++) {
            randId = ((Math.random() * 10000) | 0) + 1;
            world = sql.query(connection, 'select * from World where World.id = ' + randId)[0];
            body.push(world);
         }
         connection.close();
         return {
            status: 200,
            headers: {"Content-Type": "application/json; charset=UTF-8"},
            body: [JSON.stringify(body)]
         }
      }
   }
};


var datasource = module.singleton('pooling-datasource', function() {
  return sql.connect("jdbc:mysql://" + dbHost + "/hello_world", 'benchmarkdbuser', 'benchmarkdbpass');
});

if (require.main == module) {
    require("ringo/httpserver").main(module.id);
}
@@ -573,16 +573,31 @@
 ries');%0A
+      try %7B%0A   
       va
@@ -635,24 +635,27 @@
 nnection();%0A
+   
       if (qu
@@ -676,32 +676,35 @@
 ull) %7B%0A         
+   
 var randId = ((M
@@ -734,16 +734,19 @@
  0) + 1%0A
+   
         
@@ -826,32 +826,35 @@
 ' + randId)%5B0%5D;%0A
+   
          // let'
@@ -882,32 +882,35 @@
 o this%0A         
+   
 connection.close
@@ -914,32 +914,35 @@
 ose();%0A         
+   
 return %7B%0A       
@@ -938,32 +938,35 @@
 n %7B%0A            
+  
+ 
 status: 200,%0A   
@@ -954,32 +954,35 @@
    status: 200,%0A
+   
             head
@@ -1043,32 +1043,35 @@
 %22%7D,%0A            
+   
 body: %5BJSON.stri
@@ -1089,26 +1089,32 @@
 )%5D%0A         
+  
+ 
 %7D%0A
+   
       %7D else
@@ -1116,16 +1116,19 @@
  else %7B%0A
+   
         
@@ -1172,24 +1172,27 @@
 );%0A         
+ 
+  
 var body = %5B
@@ -1194,16 +1194,19 @@
 y = %5B%5D;%0A
+   
         
@@ -1234,16 +1234,19 @@
         
+   
 for (var
@@ -1288,16 +1288,19 @@
         
+ 
+  
 randId =
@@ -1335,16 +1335,19 @@
 0) + 1;%0A
+   
         
@@ -1426,32 +1426,35 @@
 ' + randId)%5B0%5D;%0A
+   
             body
@@ -1468,34 +1468,40 @@
 orld);%0A         
-%7D%0A
+   %7D%0A   
          connect
@@ -1505,32 +1505,35 @@
 ection.close();%0A
+   
          return 
@@ -1538,32 +1538,35 @@
 n %7B%0A            
+   
 status: 200,%0A   
@@ -1554,32 +1554,35 @@
    status: 200,%0A
+   
             head
@@ -1631,32 +1631,35 @@
 harset=UTF-8%22%7D,%0A
+   
             body
@@ -1683,16 +1683,204 @@
 (body)%5D%0A
+            %7D%0A         %7D%0A      %7D catch (e) %7B%0A         connection.close();%0A         connection = null;%0A      %7D finally %7B%0A         if (connection !== null) %7B%0A            connection.close();%0A
         

make sure we close the connection in any case