Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import ballerina/io;
- import ballerina/jdbc;
- import ballerina/sql;
- // Client endpoint for MySQL database.
- endpoint jdbc:Client testDB {
- url: "jdbc:mysql://localhost:3306/testdb",
- username: "test",
- password: "test",
- poolOptions: { maximumPoolSize: 1 }, //Only one connection in the pool
- dbOptions: { useSSL: false }
- };
- // This is the type created to represent data row.
- type Student record {
- int id;
- int age;
- string name;
- };
- public function main() {
- // This t1 uses the only avilable connection in the pool.
- table<Student> t1 = check testDB->select("SELECT * from student", Student);
- int i = 0;
- while(t1.hasNext()) {
- Student s = check <Student> t1.getNext();
- io:println(s);
- if (i == 2) {
- break;
- }
- i = i + 1;
- }
- t1.close(); //Without this close, that connection won't release back to the pool.
- // If the above line commented, below select will give an error as there is no
- // connection available to use for this select.
- table<Student> t2 = check testDB->select("SELECT * from student", Student);
- // Finally, close the connection pool.
- testDB.stop();
- }
Add Comment
Please, Sign In to add comment