Note that there are some explanatory texts on larger screens.

plurals
  1. POTomcat and mySQL configurations
    primarykey
    data
    text
    <p>I have been using Tomcat and mySQL for quite awhile but recently have been managing to max out my connections. I don't know why as my code opens and closes the connection when its done. For instance here is my connect and close methods:</p> <pre><code>public void closeConnection(){ try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } public void connect(){ try { initCtx = new InitialContext(); } catch (NamingException e) { e.printStackTrace(); } try { envCtx = (Context) initCtx.lookup("java:comp/env"); } catch (NamingException e) { e.printStackTrace(); } try { ds = (DataSource) envCtx.lookup("jdbc/TestDB"); } catch (NamingException e) { e.printStackTrace(); } } </code></pre> <p>And this is a basic method to execute a generic statement:</p> <pre><code>public void excuteStatement(String query){ connect(); try { con = ds.getConnection(); con.createStatement().executeUpdate(query); } catch (Exception ex ){ } finally { if (con != null) closeConnection(); } } </code></pre> <p>Is there any reason this sort of code should cause a connection to be left open? I get all the data I need, everything works fine 99% of the time. </p> <p>This is my configuration in the Tomcat context.xml:</p> <pre><code> &lt;Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="myUserName" password="myPass" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://xxx.xxx.xxx.xxx:0000/myUserName"/&gt; </code></pre> <p>Now at this very moment I'm locked out as I have max connections. Should I lower the wait time to avoid this? </p> <p>One thing I should say about my code, its often that it will trigger many multiple statements in large loops. I think perhaps this rapid opening and closing could be the issue. As my Tomcat servlet is always running should it just open a connection once and leave it open with checks to make sure its still active rather than always opening a connection and closing it? </p> <p>Thanks for any and all advice on this. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload