Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- response.setContentType("text/html");
- int periodNum = Integer.valueOf(request.getAttribute("periodNum2").toString());
- int firstNum = Integer.valueOf(request.getAttribute("firstNum2").toString());
- Map<String, Card> map = new HashMap();
- // fill map
- new SelectCard().execute(map);
- request.getRequestDispatcher(PAGE_RESULT).forward(request, response);
- }
- public class SelectCard {
- protected static DBConnectionPool pool = DBConnectionPool.getInstance();
- public void execute(Map<String, Card> map){
- Connection connection = pool.getConnection();
- if (connection == null) {
- System.out.println("Can't receive connection!");
- LogSingleton.appendLog("Can't receive connection!");
- }
- String SELECT_QUERY = "SELECT * from Majong.dbo.Cards where id = %d";
- Statement stmt = null;
- ResultSet resultSet= null;
- String query = String.format(Locale.ENGLISH, SELECT_QUERY,
- map.get("west").getId()
- );
- try {
- stmt = connection.createStatement(); // tomcat утверждает, что в этом месте NullPointerException
- resultSet = stmt.executeQuery(query);
- while(resultSet.next()){
- // work with result
- }
- }
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- try {
- if (stmt != null) {
- stmt.close();
- }
- if(resultSet != null){
- resultSet.close();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- pool.freeConnections(connection);
- }
- }
- }
- public class DBConnectionPool {
- static private String URL ;
- private static String user = "";
- private static String password = "";
- private static int maxConn;
- private final String DRIVER_NAME = "net.sourceforge.jtds.jdbc.Driver";
- private final String JDBC_URL = "jdbc:jtds:sqlserver://localhost:1433/Majong;";
- private final int MAX_COUNT = 3;
- PrintWriter log;
- static private List<Connection> freeConnections = new ArrayList<>();
- static private DBConnectionPool instance = new DBConnectionPool();
- private DBConnectionPool() {
- this.URL = JDBC_URL ;
- this.maxConn = MAX_COUNT;
- loadDrivers();
- }
- private void loadDrivers() {
- try {
- DriverManager.registerDriver(new net.sourceforge.jtds.jdbc.Driver());
- System.out.println("Registered JDBC driver ");
- } catch (SQLException e) {
- e.printStackTrace();
- System.out.println("Can't register JDBC driver");
- }
- }
- public static synchronized DBConnectionPool getInstance() {
- if (instance == null) {
- instance = new DBConnectionPool();
- }
- return instance;
- }
- public synchronized Connection getConnection() {
- Connection con = null;
- if (!freeConnections.isEmpty()) {
- con = (Connection)freeConnections.get(freeConnections.size()-1);
- freeConnections.remove(con);
- try {
- if (con.isClosed()) {
- System.out.println("Removed bad connection ");
- con = getConnection(); // Try again recursively
- }
- } catch (SQLException e) {
- System.out.println("Removed bad connection ");
- con = getConnection(); // Try again recursively
- } catch (Exception e) {
- System.out.println("Removed bad connection ");
- con = getConnection();// Try again recursively
- }
- } else {
- con = newConnection();
- }
- return con;
- }
- private Connection newConnection() {
- Connection con = null;
- try {
- if (user == null) {
- con = DriverManager.getConnection(URL);
- } else {
- con = DriverManager.getConnection(URL, user, password);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- return null;
- }catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- return con;
- }
- public synchronized void freeConnections(Connection con) {
- if ( (con != null) && (freeConnections.size() <= maxConn) ) {
- freeConnections.add(con);
- }
- }
- public synchronized void released() {
- Iterator allConnections = freeConnections.iterator();
- while (allConnections. hasNext()) {
- Connection con = (Connection) allConnections.next();
- try {
- con.close();
- System.out.println("Closed connection for pool");
- }
- catch (SQLException e) {
- System.out.println("Can't close connection for pool");
- }
- }
- freeConnections.clear(); }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement