Advertisement
baronborca

prvizadatak

Sep 10th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.22 KB | None | 0 0
  1. package database;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5.  
  6. public class DB {
  7.     private static DB instance;
  8.     private static final int MAX_CON = 5;
  9.     private static final Connection[] bafer = new Connection[MAX_CON];
  10.     private int first = 0, last = 0, free = MAX_CON;
  11.    
  12.     private DB(){
  13.         try {
  14.             Class.forName("com.mysql.jdbc.Driver");
  15.             for(int i = 0; i < MAX_CON; i++){
  16.                 bafer[i] = DriverManager.getConnection("jdbc:mysql://localhost:3306/korisnikinfo", "root", "");
  17.             }
  18.         } catch (Exception e) {
  19.             e.printStackTrace();
  20.         }
  21.            
  22.     }
  23.    
  24.     public static DB getInstance(){
  25.         if(instance == null)
  26.             instance = new DB();
  27.         return instance;
  28.     }
  29.    
  30.     public synchronized Connection getConnection(){
  31.         if(free == 0)
  32.             return null;
  33.         free--;
  34.         Connection con = bafer[first];
  35.         first = (first + 1) % MAX_CON;
  36.         return con;
  37.     }
  38.    
  39.     public synchronized void putConnection(Connection con){
  40.         if(con == null)
  41.             return;
  42.         free++;
  43.         bafer[last] = con;
  44.         last = (last + 1) % MAX_CON;
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement