Advertisement
Guest User

Untitled

a guest
Jun 16th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.68 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.PreparedStatement;
  3. import java.time.Instant;
  4. import java.util.Properties;
  5. import java.util.concurrent.ExecutorService;
  6. import java.util.concurrent.Executors;
  7. import java.util.concurrent.atomic.AtomicInteger;
  8. import java.util.logging.Logger;
  9.  
  10. import com.mchange.v2.c3p0.ComboPooledDataSource;
  11.  
  12. public class Example {
  13.  
  14.     // singleton implementation, safe-thread
  15.  
  16.     private static class Singleton {
  17.         private static final Example INSTANCE = new Example();
  18.     }
  19.  
  20.     public static final Example getInstance() {
  21.         return Singleton.INSTANCE;
  22.     }
  23.  
  24.     // class implementation
  25.  
  26.     private static final String DB_DRIVER_CLASS;
  27.     private static final String DB_JDBC_URL;
  28.     private static final String DB_USER;
  29.     private static final String DB_PASSWORD;
  30.  
  31.     static {
  32.         DB_DRIVER_CLASS = "oracle.jdbc.driver.OracleDriver";
  33.         DB_JDBC_URL = "jdbc:oracle:thin:@//localhost:1521/XE";
  34.         DB_USER = "user";
  35.         DB_PASSWORD = "pass";
  36.     }
  37.  
  38.     private ComboPooledDataSource pool;
  39.     Connection connection;
  40.     private boolean isConnected;
  41.  
  42.     {
  43.         this.pool = new ComboPooledDataSource();
  44.  
  45.         try {
  46.             this.pool.setDriverClass(DB_DRIVER_CLASS);
  47.             this.pool.setJdbcUrl(DB_JDBC_URL);
  48.             this.pool.setUser(DB_USER);
  49.             this.pool.setPassword(DB_PASSWORD);
  50.  
  51.             this.pool.setMinPoolSize(1);
  52.             this.pool.setAcquireIncrement(5);
  53.             this.pool.setMaxPoolSize(3);
  54.             // this.pool.setUnreturnedConnectionTimeout(1);
  55.             // this.pool.setDebugUnreturnedConnectionStackTraces(true);
  56.             // this.pool.setMaxStatements(180);
  57.  
  58.             this.isConnected = true;
  59.         } catch (Exception e) {
  60.             this.isConnected = false;
  61.             e.printStackTrace();
  62.         }
  63.     }
  64.  
  65.     public boolean isConnected() {
  66.         return this.isConnected;
  67.     }
  68.  
  69.     // if false, get connection is limited up to this.pool.setMaxPoolSize(?);
  70.     // this call is block until connections are freed
  71.     public Connection getConnection(boolean recycle) throws Exception {
  72.         if (recycle) {
  73.             if (this.connection == null && recycle)
  74.                 this.connection = this.pool.getConnection();
  75.             return this.connection;
  76.         } else {
  77.             return this.pool.getConnection();
  78.         }
  79.     }
  80.  
  81.     // main
  82.  
  83.     public static void main(String[] args) throws Exception {
  84.         Properties properties = new Properties(System.getProperties());
  85.         properties.setProperty("ACS.logstdout", "04");
  86.         properties.setProperty("net.sf.ehcache.skipUpdateCheck", "false");
  87.         properties.setProperty("com.mchange.v2.log.MLog", "com.mchange.v2.log.FallbackMLog");
  88.         properties.setProperty("com.mchange.v2.log.FallbackMLog.DEFAULT_CUTOFF_LEVEL", "OFF");
  89.         properties.setProperty("java.util.logging.SimpleFormatter.format", "%1$tFT%1$tT %4$s [] %5$s%6$s%n");
  90.         System.setProperties(properties);
  91.  
  92.         final boolean recycle = false;
  93.         final AtomicInteger counter = new AtomicInteger(0);
  94.         final Logger logger = Logger.getAnonymousLogger();
  95.  
  96.         ExecutorService executorService = Executors.newFixedThreadPool(50);
  97.  
  98.         final String sql = "insert into table1 (recordid) values(?)";
  99.  
  100.         int limit = 10;
  101.         while (limit-- > 0) {
  102.             executorService.submit(() -> {
  103.  
  104.                 final int localCounter = counter.getAndIncrement();
  105.                 final long milli = Instant.now().toEpochMilli();
  106.                 final String threadName = Thread.currentThread().getName();
  107.  
  108.                 final String message = String.format("threadId = %s\t-> counter = %d", threadName, localCounter);
  109.                
  110.                
  111.                 try (Connection connection = Example.getInstance().getConnection(recycle)) {
  112.                     logger.info(String.format("%s\t-> %s", message, "connected"));
  113.  
  114.                     try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
  115.                         int inner = 20;
  116.                         while (inner-- > 0) {
  117.                             String recordid = String.format("%s_%d_%d_%d", threadName, localCounter, milli, inner);
  118.                             preparedStatement.setString(1, recordid);
  119.                             preparedStatement.addBatch();
  120.                             // logger.info(String.format("%s\t-> %s", message, sql));
  121.                         }
  122.                        
  123.                         int insertions = preparedStatement.executeBatch().length;
  124.                         logger.info(String.format("%s\t-> %s : %d", message, "inserted", insertions));
  125.                     } catch (Exception e) {
  126.                         e.printStackTrace();
  127.                     }
  128.  
  129.                 } catch (Exception e) {
  130.                     logger.severe(String.format("%s\t-> %s", message, e.toString()));
  131.                     e.printStackTrace();
  132.                 }
  133.  
  134.                 logger.info(String.format("%s\t-> %s", message, "exiting"));
  135.             });
  136.         }
  137.  
  138.         executorService.shutdown();
  139.         // Thread.sleep(480_000);
  140.     }
  141. }
  142.  
  143. /*
  144.  
  145. 2017-06-16T12:18:27 INFO [] threadId = pool-1-thread-4  -> counter = 3  -> connected
  146. 2017-06-16T12:18:27 INFO [] threadId = pool-1-thread-8  -> counter = 7  -> connected
  147. 2017-06-16T12:18:27 INFO [] threadId = pool-1-thread-2  -> counter = 1  -> connected
  148. 2017-06-16T12:18:28 INFO [] threadId = pool-1-thread-2  -> counter = 1  -> inserted : 20
  149. 2017-06-16T12:18:28 INFO [] threadId = pool-1-thread-4  -> counter = 3  -> inserted : 20
  150. 2017-06-16T12:18:28 INFO [] threadId = pool-1-thread-8  -> counter = 7  -> inserted : 20
  151. 2017-06-16T12:18:28 INFO [] threadId = pool-1-thread-4  -> counter = 3  -> exiting
  152. 2017-06-16T12:18:28 INFO [] threadId = pool-1-thread-7  -> counter = 4  -> connected
  153. 2017-06-16T12:18:28 INFO [] threadId = pool-1-thread-8  -> counter = 7  -> exiting
  154. 2017-06-16T12:18:28 INFO [] threadId = pool-1-thread-6  -> counter = 6  -> connected
  155. 2017-06-16T12:18:28 INFO [] threadId = pool-1-thread-1  -> counter = 0  -> connected
  156. 2017-06-16T12:18:28 INFO [] threadId = pool-1-thread-2  -> counter = 1  -> exiting
  157. 2017-06-16T12:18:28 INFO [] threadId = pool-1-thread-7  -> counter = 4  -> inserted : 20
  158. 2017-06-16T12:18:28 INFO [] threadId = pool-1-thread-1  -> counter = 0  -> inserted : 20
  159. 2017-06-16T12:18:28 INFO [] threadId = pool-1-thread-7  -> counter = 4  -> exiting
  160. 2017-06-16T12:18:28 INFO [] threadId = pool-1-thread-9  -> counter = 8  -> connected
  161. 2017-06-16T12:18:28 INFO [] threadId = pool-1-thread-1  -> counter = 0  -> exiting
  162. 2017-06-16T12:18:28 INFO [] threadId = pool-1-thread-10 -> counter = 9  -> connected
  163. 2017-06-16T12:18:28 INFO [] threadId = pool-1-thread-6  -> counter = 6  -> inserted : 20
  164. 2017-06-16T12:18:28 INFO [] threadId = pool-1-thread-6  -> counter = 6  -> exiting
  165. 2017-06-16T12:18:28 INFO [] threadId = pool-1-thread-5  -> counter = 5  -> connected
  166. 2017-06-16T12:18:28 INFO [] threadId = pool-1-thread-10 -> counter = 9  -> inserted : 20
  167. 2017-06-16T12:18:28 INFO [] threadId = pool-1-thread-9  -> counter = 8  -> inserted : 20
  168. 2017-06-16T12:18:28 INFO [] threadId = pool-1-thread-10 -> counter = 9  -> exiting
  169. 2017-06-16T12:18:28 INFO [] threadId = pool-1-thread-9  -> counter = 8  -> exiting
  170. 2017-06-16T12:18:28 INFO [] threadId = pool-1-thread-3  -> counter = 2  -> connected
  171. 2017-06-16T12:18:28 INFO [] threadId = pool-1-thread-3  -> counter = 2  -> inserted : 20
  172. 2017-06-16T12:18:28 INFO [] threadId = pool-1-thread-5  -> counter = 5  -> inserted : 20
  173. 2017-06-16T12:18:28 INFO [] threadId = pool-1-thread-5  -> counter = 5  -> exiting
  174. 2017-06-16T12:18:28 INFO [] threadId = pool-1-thread-3  -> counter = 2  -> exiting
  175.  
  176.  
  177. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement