Guest User

Untitled

a guest
Sep 6th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. Closing JDBC Connections in Pool
  2. Connection conn = getConnection(...);
  3. Statement stmt = conn.conn.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE,
  4. ResultSet.CONCUR_READ_ONLY);
  5. ResultSet rset = stmt.executeQuery (sqlQuery);
  6.  
  7. // do stuff with rset
  8.  
  9. rset.close(); stmt.close(); conn.close();
  10.  
  11. synchronized public Connection getConnection (boolean pooledConnection)
  12. throws SQLException {
  13. if (pooledConnection) {
  14. if (ds == null) {
  15. try {
  16. Context envCtx = (Context)
  17. new InitialContext().lookup("java:comp/env");
  18. ds = (DataSource) envCtx.lookup("jdbc/NamedInTomcat");
  19. return ds.getConnection();
  20. } catch (NamingException e) {
  21. e.printStackTrace();
  22. }}
  23. return (ds == null) ? getConnection (false) : ds.getConnection();
  24. }
  25. return DriverManager.getConnection(
  26. "jdbc:mysql://"+ipaddy+":"+dbPort +"/" + dbName, uName, pWord);
  27. }
  28.  
  29. synchronized public Connection getTomcatConnection () {...}
  30. public Connection getCustomConnection() {...}
  31.  
  32. public void close() throws SQLException {
  33. pool.returnConnection(this);
  34. }
Add Comment
Please, Sign In to add comment