Advertisement
stonar96

Connection Pool Tutorial UpdateBukkitRunnable

Jan 31st, 2016
1,226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.29 KB | None | 0 0
  1. public class UpdateBukkitRunnable extends BukkitRunnable {
  2.     private final DataSource dataSource;
  3.     private final String statement;
  4.     private final Callback<Integer, SQLException> callback;
  5.    
  6.     public UpdateBukkitRunnable(DataSource dataSource, String statement, @Nullable Callback<Integer, SQLException> callback) {
  7.         if (dataSource == null) {
  8.             //TODO: IllegalArgumentException
  9.         }
  10.        
  11.         if (statement == null) {
  12.             //TODO: IllegalArgumentException
  13.         }
  14.        
  15.         this.dataSource = dataSource;
  16.         this.statement = statement;
  17.         this.callback = callback;
  18.     }
  19.    
  20.     @Override
  21.     public void run() {
  22.         Connection connection = null;
  23.         PreparedStatement preparedStatement = null;
  24.        
  25.         try {
  26.             connection = dataSource.getConnection();
  27.             preparedStatement = connection.prepareStatement(statement);
  28.            
  29.             if (callback != null) {
  30.                 callback.call(preparedStatement.executeUpdate(), null);
  31.             }
  32.         } catch (SQLException e) {
  33.             if (callback != null) {
  34.                 callback.call(null, e);
  35.             }
  36.         } finally {
  37.             if (preparedStatement != null) {
  38.                 try {
  39.                     preparedStatement.close();
  40.                 } catch (SQLException e) {
  41.                     e.printStackTrace();
  42.                 }
  43.             }
  44.            
  45.             if (connection != null) {
  46.                 try {
  47.                     connection.close();
  48.                 } catch (SQLException e) {
  49.                     e.printStackTrace();
  50.                 }
  51.             }
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement