从WildFly数据源关闭连接会导致java.net.SocketException

时间:2016-07-11 14:22:53

标签: jdbc datasource wildfly wildfly-8

我正在使用WildFly 10.0.0和MySQL 5.5.17以及JDBC驱动程序5.1.39

我首先在WildFly中将DataSouce设置为模块

在文件夹WILDFLY_HOME \ modules \ system \ layers \ base \ com \ mysql \ main中有驱动程序和文件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>

配置文件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>

当我在wildfly控制台中测试连接时,我收到消息:&#34;成功创建了JDBC连接。 已成功连接到数据库MyDS&#34;

当我尝试使用此DataSource时:

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

我有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

当我这样做时:

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

它有效......

我如何处理DataSource?如果我不关闭Connection,会出现问题吗?也许这是DataSource配置问题......

0 个答案:

没有答案