Advertisement
stonar96

Connection Pool Tutorial example

Jan 31st, 2016
2,457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.40 KB | None | 0 0
  1.     @Override
  2.     public boolean onCommand(final CommandSender sender, Command cmd, String label, String[] args) {
  3.         if (cmd.getName().equalsIgnoreCase("testupdate")) {
  4.             if (args.length == 0) {
  5.                 sender.sendMessage("Starting an asynchronous task.");
  6.                
  7.                 new BukkitRunnable() {
  8.                     @Override
  9.                     public void run() {
  10.                         sender.sendMessage("Asynchronous task started.");
  11.                         sender.sendMessage("Starting database transacation.");
  12.                         Connection connection = null;
  13.                         PreparedStatement preparedStatement = null;
  14.                        
  15.                         try {
  16.                             connection = dataSource.getConnection();
  17.                             preparedStatement = connection.prepareStatement("INSERT INTO Test (Key, Value) VALUES ('0', '10')");
  18.                             int rows = preparedStatement.executeUpdate();
  19.                             sender.sendMessage("Transaction succeeded. Inserted: " + rows);
  20.                             // WARNING! If you want to call not thread-safe API methods here, put it into a synchronous task!
  21.                         } catch (SQLException e) {
  22.                             sender.sendMessage("Transaction failed.");
  23.                         } finally {
  24.                             sender.sendMessage("Closing resources.");
  25.                            
  26.                             if (preparedStatement != null) {
  27.                                 try {
  28.                                     preparedStatement.close();
  29.                                 } catch (SQLException e) {
  30.                                     sender.sendMessage("Error while closing statement.");
  31.                                 }
  32.                             }
  33.                            
  34.                             if (connection != null) {
  35.                                 try {
  36.                                     connection.close();
  37.                                 } catch (SQLException e) {
  38.                                     sender.sendMessage("Error while closing connection.");
  39.                                 }
  40.                             }
  41.                         }
  42.                     }
  43.                 }.runTaskAsynchronously(this);
  44.             }
  45.         } else if (cmd.getName().equalsIgnoreCase("testquery")) {
  46.             if (args.length == 0) {
  47.                 sender.sendMessage("Starting an asynchronous task.");
  48.                
  49.                 new BukkitRunnable() {
  50.                     @Override
  51.                     public void run() {
  52.                         sender.sendMessage("Asynchronous task started.");
  53.                         sender.sendMessage("Starting database transaction.");
  54.                         Connection connection = null;
  55.                         PreparedStatement preparedStatement = null;
  56.                         ResultSet resultSet = null;
  57.                        
  58.                         try {
  59.                             connection = dataSource.getConnection();
  60.                             preparedStatement = connection.prepareStatement("SELECT * FROM Test WHERE Key = '0'");
  61.                             resultSet = preparedStatement.executeQuery();
  62.                            
  63.                             if (resultSet.next()) {
  64.                                 sender.sendMessage("Transaction succeeded. Value: " + resultSet.getInt("Value"));
  65.                                 // WARNING! If you want to call not thread-safe API methods here, put it into a synchronous task!
  66.                             } else {
  67.                                 sender.sendMessage("Transaction succeeded. No value.");
  68.                             }
  69.                         } catch (SQLException e) {
  70.                             sender.sendMessage("Transaction failed.");
  71.                         } finally {
  72.                             sender.sendMessage("Closing resources.");
  73.  
  74.                             if (resultSet != null) {
  75.                                 try {
  76.                                     resultSet.close();
  77.                                 } catch (SQLException e) {
  78.                                     sender.sendMessage("Error while closing result set.");
  79.                                 }
  80.                             }
  81.                            
  82.                             if (preparedStatement != null) {
  83.                                 try {
  84.                                     preparedStatement.close();
  85.                                 } catch (SQLException e) {
  86.                                     sender.sendMessage("Error while closing statement.");
  87.                                 }
  88.                             }
  89.                            
  90.                             if (connection != null) {
  91.                                 try {
  92.                                     connection.close();
  93.                                 } catch (SQLException e) {
  94.                                     sender.sendMessage("Error while closing connection.");
  95.                                 }
  96.                             }
  97.                         }
  98.                     }
  99.                 }.runTaskAsynchronously(this);
  100.             }
  101.         }
  102.        
  103.         return false;
  104.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement