Advertisement
Guest User

Untitled

a guest
Mar 18th, 2016
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  2. response.setContentType("text/html");
  3.  
  4. int periodNum = Integer.valueOf(request.getAttribute("periodNum2").toString());
  5. int firstNum = Integer.valueOf(request.getAttribute("firstNum2").toString());
  6. Map<String, Card> map = new HashMap();
  7. // fill map
  8.  
  9. new SelectCard().execute(map);
  10. request.getRequestDispatcher(PAGE_RESULT).forward(request, response);
  11. }
  12.  
  13. public class SelectCard {
  14. protected static DBConnectionPool pool = DBConnectionPool.getInstance();
  15.  
  16. public void execute(Map<String, Card> map){
  17. Connection connection = pool.getConnection();
  18. if (connection == null) {
  19. System.out.println("Can't receive connection!");
  20. LogSingleton.appendLog("Can't receive connection!");
  21. }
  22. String SELECT_QUERY = "SELECT * from Majong.dbo.Cards where id = %d";
  23.  
  24. Statement stmt = null;
  25. ResultSet resultSet= null;
  26. String query = String.format(Locale.ENGLISH, SELECT_QUERY,
  27. map.get("west").getId()
  28. );
  29. try {
  30. stmt = connection.createStatement(); // tomcat утверждает, что в этом месте NullPointerException
  31. resultSet = stmt.executeQuery(query);
  32. while(resultSet.next()){
  33. // work with result
  34. }
  35. }
  36. }
  37.  
  38. } catch (SQLException e) {
  39. e.printStackTrace();
  40. } finally {
  41.  
  42. try {
  43. if (stmt != null) {
  44. stmt.close();
  45. }
  46. if(resultSet != null){
  47. resultSet.close();
  48. }
  49. } catch (SQLException e) {
  50. e.printStackTrace();
  51. }
  52. pool.freeConnections(connection);
  53. }
  54. }
  55. }
  56.  
  57. public class DBConnectionPool {
  58.  
  59. static private String URL ;
  60. private static String user = "";
  61. private static String password = "";
  62. private static int maxConn;
  63. private final String DRIVER_NAME = "net.sourceforge.jtds.jdbc.Driver";
  64. private final String JDBC_URL = "jdbc:jtds:sqlserver://localhost:1433/Majong;";
  65. private final int MAX_COUNT = 3;
  66.  
  67. PrintWriter log;
  68. static private List<Connection> freeConnections = new ArrayList<>();
  69. static private DBConnectionPool instance = new DBConnectionPool();
  70.  
  71. private DBConnectionPool() {
  72. this.URL = JDBC_URL ;
  73. this.maxConn = MAX_COUNT;
  74. loadDrivers();
  75. }
  76.  
  77. private void loadDrivers() {
  78. try {
  79. DriverManager.registerDriver(new net.sourceforge.jtds.jdbc.Driver());
  80. System.out.println("Registered JDBC driver ");
  81. } catch (SQLException e) {
  82. e.printStackTrace();
  83. System.out.println("Can't register JDBC driver");
  84. }
  85. }
  86.  
  87. public static synchronized DBConnectionPool getInstance() {
  88.  
  89. if (instance == null) {
  90. instance = new DBConnectionPool();
  91. }
  92.  
  93. return instance;
  94. }
  95.  
  96.  
  97. public synchronized Connection getConnection() {
  98.  
  99. Connection con = null;
  100. if (!freeConnections.isEmpty()) {
  101. con = (Connection)freeConnections.get(freeConnections.size()-1);
  102. freeConnections.remove(con);
  103.  
  104. try {
  105. if (con.isClosed()) {
  106. System.out.println("Removed bad connection ");
  107. con = getConnection(); // Try again recursively
  108. }
  109. } catch (SQLException e) {
  110. System.out.println("Removed bad connection ");
  111. con = getConnection(); // Try again recursively
  112. } catch (Exception e) {
  113. System.out.println("Removed bad connection ");
  114. con = getConnection();// Try again recursively
  115. }
  116. } else {
  117. con = newConnection();
  118. }
  119.  
  120. return con;
  121. }
  122. private Connection newConnection() {
  123.  
  124. Connection con = null;
  125. try {
  126. if (user == null) {
  127. con = DriverManager.getConnection(URL);
  128. } else {
  129. con = DriverManager.getConnection(URL, user, password);
  130. }
  131. } catch (SQLException e) {
  132. e.printStackTrace();
  133. return null;
  134. }catch (ClassNotFoundException e) {
  135. e.printStackTrace();
  136. }
  137.  
  138. return con;
  139. }
  140. public synchronized void freeConnections(Connection con) {
  141.  
  142. if ( (con != null) && (freeConnections.size() <= maxConn) ) {
  143. freeConnections.add(con);
  144. }
  145. }
  146. public synchronized void released() {
  147. Iterator allConnections = freeConnections.iterator();
  148.  
  149. while (allConnections. hasNext()) {
  150. Connection con = (Connection) allConnections.next();
  151.  
  152. try {
  153. con.close();
  154. System.out.println("Closed connection for pool");
  155. }
  156.  
  157. catch (SQLException e) {
  158. System.out.println("Can't close connection for pool");
  159. }
  160.  
  161. }
  162.  
  163. freeConnections.clear(); }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement