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...