Closing Connection from WildFly Datasource cause java.net.SocketException
<p>I'm using WildFly 10.0.0 and MySQL 5.5.17 and JDBC Driver 5.1.39</p>

<p>I first set a <code>DataSouce</code> in WildFly as a module</p>

<p>in the folder WILDFLY_HOME\modules\system\layers\base\com\mysql\main there is the driver and the file <code>module.xml</code> :</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;module xmlns="urn:jboss:module:1.1" name="com.mysql"&gt;
    &lt;resources&gt;
        &lt;resource-root path="mysql-connector-java-5.1.39-bin.jar"/&gt;
    &lt;/resources&gt;
    &lt;dependencies&gt;
        &lt;module name="javax.api"/&gt;
        &lt;module name="javax.transaction.api"/&gt;
    &lt;/dependencies&gt;
&lt;/module&gt;
</code></pre>

<p>And the config file <code>standalone-full.xml</code> :</p>

<pre><code>    &lt;subsystem xmlns="urn:jboss:domain:datasources:4.0"&gt;
        &lt;datasources&gt;
            &lt;datasource jta="false" jndi-name="java:/jdbc/MyDS" pool-name="MyDS" enabled="true" use-ccm="false"&gt;
                &lt;connection-url&gt;jdbc:mysql://www.my-url.com:3306/my_db&lt;/connection-url&gt;
                &lt;driver&gt;mysql&lt;/driver&gt;
                &lt;pool&gt;
                    &lt;min-pool-size&gt;5&lt;/min-pool-size&gt;
                    &lt;max-pool-size&gt;20&lt;/max-pool-size&gt;
                    &lt;prefill&gt;true&lt;/prefill&gt;
                &lt;/pool&gt;
                &lt;security&gt;
                    &lt;user-name&gt;login&lt;/user-name&gt;
                    &lt;password&gt;password&lt;/password&gt;
                &lt;/security&gt;
            &lt;/datasource&gt;
            &lt;drivers&gt;
                &lt;driver name="mysql" module="com.mysql"&gt;
                    &lt;driver-class&gt;com.mysql.jdbc.Driver&lt;/driver-class&gt;
                    &lt;xa-datasource-class&gt;com.mysql.jdbc.Driver&lt;/xa-datasource-class&gt;
                &lt;/driver&gt;
            &lt;/drivers&gt;
        &lt;/datasources&gt;
    &lt;/subsystem&gt;
</code></pre>

<p>When I test the connection within the wildfly console I have the message : "Successfully created JDBC connection.
Successfully connected to database MyDS"</p>

<p>When I try to use this <code>DataSource</code> doing :</p>

<pre><code>@Resource(mappedName = "java:/jdbc/MyDS")
private DataSource dataSource;

private Integer selectId(String mail) {
    try (Connection connection = dataSource.getConnection()) {
        try (PreparedStatement statement = connection.prepareStatement(getQuery())) {
            statement.setString(1, mail);
            try (ResultSet resultSet = statement.executeQuery()) {
                if (resultSet.next()) {
                    Integer id = resultSet.getInt(1);
                    return id;
                } else {
                    return null;
                }
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
</code></pre>

<p>I have this <code>Exception</code> :</p>

<pre><code>The last packet successfully received from the server was 2 621 144 milliseconds ago.  The last packet sent successfully to the server was 2 621 171 milliseconds ago.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:988)
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3739)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2508)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2673)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2549)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1962)
at org.jboss.jca.adapters.jdbc.WrappedPreparedStatement.executeQuery(WrappedPreparedStatement.java:504)
at my.app.mail.service.MailService.selectId(LoginService.java:46)
... 129 more
Caused by: java.net.SocketException: Connection reset by peer: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3721)
... 136 more
</code></pre>

<p>When I do :</p>

<pre><code>@Resource(mappedName = "java:/jdbc/MyDS")
private DataSource dataSource;

private Integer selectId(String mail) {
    try {
        Connection connection = dataSource.getConnection();
        try (PreparedStatement statement = connection.prepareStatement(getQuery())) {
            statement.setString(1, mail);
            try (ResultSet resultSet = statement.executeQuery()) {
                if (resultSet.next()) {
                    Integer id = resultSet.getInt(1);
                    return id;
                } else {
                    return null;
                }
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
</code></pre>

<p>It works...</p>

<p>How I have to handle <code>DataSource</code>? Do problems will occurs if I don't close <code>Connection</code>? Maybe it's a problem of <code>DataSource</code> config...</p>