0

I'm using WildFly 10.0.0 and MySQL 5.5.17 and JDBC Driver 5.1.39

I first set a DataSouce in WildFly as a module

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

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

And the config file standalone-full.xml :

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

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

When I try to use this DataSource doing :

@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);
    }
}

I have this Exception :

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

When I do :

@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);
    }
}

It works...

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

kwisatz
  • 1,266
  • 3
  • 16
  • 36
  • You're not closing your connection so maybe you're running out of connections. – James R. Perkins Jul 12 '16 at 22:29
  • He is closing his connection. It's wrapped in a try-with-resources block, so it will close on exit from the block. It may be that he's not meant to close the connection though, as the application server may wish it to remain open in the connection pool. – ManoDestra Jul 22 '16 at 15:49
  • This exception is not caused by closing the connection. It is caused during `executeQuery()`. See the stack trace. – user207421 Jul 25 '16 at 09:42
  • So why not closing the connection solve the issue? – kwisatz Jul 25 '16 at 10:00
  • I didn't say it was caused by closing connection, I said that not closing connection solve the issue : I tried again and I don't know how but that the case. As I said in my question, it's maybe a configuration problem... – kwisatz Jul 28 '16 at 07:50
  • i think the correct use of try with resources is embracing connection with parenthesis and put it before the curly braces, the same way you did with prepared statement – erickdeoliveiraleal Jul 17 '18 at 22:44

0 Answers0