Guest User

Untitled

a guest
Sep 23rd, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. import ballerina/io;
  2. import ballerina/jdbc;
  3. import ballerina/sql;
  4.  
  5. // Client endpoint for MySQL database.
  6. endpoint jdbc:Client testDB {
  7. url: "jdbc:mysql://localhost:3306/testdb",
  8. username: "test",
  9. password: "test",
  10. poolOptions: { maximumPoolSize: 1 }, //Only one connection in the pool
  11. dbOptions: { useSSL: false }
  12. };
  13.  
  14. // This is the type created to represent data row.
  15. type Student record {
  16. int id;
  17. int age;
  18. string name;
  19. };
  20.  
  21. public function main() {
  22. // This t1 uses the only avilable connection in the pool.
  23. table<Student> t1 = check testDB->select("SELECT * from student", Student);
  24. int i = 0;
  25. while(t1.hasNext()) {
  26. Student s = check <Student> t1.getNext();
  27. io:println(s);
  28. if (i == 2) {
  29. break;
  30. }
  31. i = i + 1;
  32. }
  33. t1.close(); //Without this close, that connection won't release back to the pool.
  34. // If the above line commented, below select will give an error as there is no
  35. // connection available to use for this select.
  36. table<Student> t2 = check testDB->select("SELECT * from student", Student);
  37. // Finally, close the connection pool.
  38. testDB.stop();
  39. }
Add Comment
Please, Sign In to add comment