reconnect to database if connection restart - java

I have JBoss, and I am using JNDI to connect to database, but when database restarts, I have to restart JBoss to take new connection pool, are there any properties to do reconnection?, please help
<subsystem xmlns="urn:jboss:domain:datasources:1.2">
<datasources>
<datasource jta="false" jndi-name="java:jboss/datasources/ams" pool-name="OracleDS" enabled="true" use-ccm="false">
<connection-url>jdbc:oracle:thin:#rhhqrac01scant:1521/testingenv</connection-url>
<driver-class>oracle.jdbc.OracleDriver</driver-class>
<driver>oracle</driver>
<pool>
<min-pool-size>100</min-pool-size>
<max-pool-size>150</max-pool-size>
<prefill>true</prefill>
<flush-strategy>FailingConnectionOnly</flush-strategy>
</pool>
<security>
<user-name>ams</user-name>
<password>testpassword</password>
</security>
<validation>
<check-valid-connection-sql>select 1 from dual</check-valid-connection-sql>
<validate-on-match>false</validate-on-match>
<background-validation>false</background-validation>
</validation>
<timeout>
<set-tx-query-timeout>false</set-tx-query-timeout>
<blocking-timeout-millis>0</blocking-timeout-millis>
<idle-timeout-minutes>0</idle-timeout-minutes>
<query-timeout>0</query-timeout>
<use-try-lock>0</use-try-lock>
<allocation-retry>0</allocation-retry>
<allocation-retry-wait-millis>0</allocation-retry-wait-millis>
</timeout>
<statement>
<share-prepared-statements>false</share-prepared-statements>
</statement>
</datasource>
<drivers>
<driver name="oracle" module="com.oracle.ojdbc6">
<xa-datasource-class>oracle.jdbc.OracleDriver</xa-datasource-class>
</driver>
</drivers>
</datasources>
</subsystem>

You can add that under your datasource tag
<check-valid-connection-sql>select 1</check-valid-connection-sql>

Try this
<validate-on-match>true </validate-on-match>
knowing that this recovers connection, but unfortunately, it will put more load on the database because it validates the connection each time the connection is checked out from the pool.

Related

ERROR [org.jboss.as.controller.management-operation] Operation (“add”) failed-address: ([(“subsystem” => "metrics")]): java.lang.NullPointerException

I am trying to deploy Keycloak in my local pc with mysql
However, when I try to run standalone.sh It gives me following error :
Here is the database configurations I did
<spi name="connectionsJpa">
<provider name="default" enabled="true">
<properties>
<property name="dataSource" value="java:jboss/datasources/KeycloakDS"/>
<!-- <property name="initializeEmpty" value="true"/> -->
<!-- <property name="migrationStrategy" value="update"/> -->
<property name="initializeEmpty" value="false"/>
<property name="migrationStrategy" value="manual"/>
<property name="migrationExport" value="${jboss.home.dir}/keycloak-
database-update.sql"/>
</properties>
</provider>
</spi>
Keycloak Datasource changes :-
<datasources>
<datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS"
enabled="true" use-java-context="true" statistics-
enabled="${wildfly.datasources.statistics-enabled:${wildfly.statistics-
enabled:false}}">
<connection-
url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE</connection-url>
<driver>h2</driver>
<security>
<user-name>sa</user-name>
<password>sa</password>
</security>
</datasource>
<!--- <datasource jndi-name="java:jboss/datasources/KeycloakDS" pool-
name="KeycloakDS" enabled="true" use-java-context="true" statistics-
enabled="${wildfly.datasources.statistics-enabled:${wildfly.statistics-enabled:false}}">
<connection-
url>jdbc:h2:${jboss.server.data.dir}/keycloak;AUTO_SERVER=TRUE</connection-url>
<driver>h2</driver>
<security>
<user-name>sa</user-name>
<password>sa</password>
</security>
</datasource> -->
<!-- mysql -->
<datasource jndi-name="java:/jboss/datasources/KeycloakDS" pool-name="KeycloakDS"
enabled="true" use-java-context="true" statistics-
enabled="${wildfly.datasources.statistics-enabled:${wildfly.statistics-
enabled:false}}">
<connection-url>jdbc:mysql://localhost:3306/keycloakdata?
useSSL=false&characterEncoding=UTF-8</connection-url>
<driver>mysql</driver>
<pool>
<min-pool-size>5</min-pool-size>
<max-pool-size>15</max-pool-size>
</pool>
<security>
<user-name>user</user-name>
<password>user</password>
</security>
<validation>
<valid-connection-checker class-
name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker"/>
<validate-on-match>true</validate-on-match>
<exception-sorter class-
name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter"/>
</validation>
</datasource>
Declare and Load JDBC Driver
<drivers>
<driver name="h2" module="com.h2database.h2">
<xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
</driver>
<driver name="mysql" module="com.mysql">
<driver-class>com.mysql.jdbc.Driver</driver-class>
</driver>
</drivers>
I also added module and its dependency to jboss (jboss-cli.sh)
module add --name=com.mysql --resources=/keycloak-
13.0.1/modules/system/layers/keycloak/com/mysql/main/mysql-connector-java-8.0.23.jar --
dependencies=javax.api,javax.transaction.api
increase keyclaok server’s timeout
<local-cache name="actionTokens">
<heap-memory size="-1"/>
<expiration max-idle="-1" interval="900000"/>
</local-cache>
<session-bean>
<stateless>
<bean-instance-pool-ref pool-name="slsb-strict-max-pool"/>
</stateless>
<stateful default-access-timeout="20000" cache-ref="simple" passivation-disabled-
cache-ref="simple"/>
<singleton default-access-timeout="20000"/>
</session-bean>
<pools>
<bean-instance-pools>
<strict-max-pool name="mdb-strict-max-pool" derive-size="from-cpu-count"
instance-acquisition-timeout="20" instance-acquisition-timeout-
unit="MINUTES"/>
<strict-max-pool name="slsb-strict-max-pool" derive-size="from-worker-pools"
instance-acquisition-timeout="20" instance-acquisition-timeout-unit="MINUTES"/>
</bean-instance-pools>
</pools>
I refered the following articles when trying this
Connect mysql server to keycloak , Setting up Keycloak Standalone with MySQL Database and Relational Database Setup in Server Installation and Configuration Guide
Full stack trace as below :

Issues while fetching result set in the jboss application

Using the below jboss configuration while fetching the data from resultset it takes more time but using standalone application it's executed quickly.
it takes 20 seconds in jboss and 0.98 seconds in standalone application
I am using database SQL Server 2019
configuration:
<datasource jta="true" jndi-name="java:jboss/datasources/FXDS" pool-name="FXDS" enabled="true" spy="false" statistics-enabled="true">
<connection-url>jdbc:sqlserver://10.10.10.10:1433;DatabaseName=SQLDB;</connection-url>
<driver-class>com.microsoft.sqlserver.jdbc.SQLServerDriver</driver-class>
<driver>sqlserver</driver>
<pool>
<min-pool-size>10</min-pool-size>
<max-pool-size>200</max-pool-size>
<prefill>true</prefill>
<flush-strategy>Gracefully</flush-strategy>
</pool>
<security>
<user-name>SA</user-name>
<password>password</password>
</security>
<validation>
<valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.mssql.MSSQLValidConnectionChecker"/>
<background-validation>true</background-validation>
<exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.mssql.MSSQLExceptionSorter"/>
</validation>
<timeout>
<set-tx-query-timeout>true</set-tx-query-timeout>
<query-timeout>1800</query-timeout>
</timeout>
</datasource>

Wildfly 10 constantly loses connection to Postgres datasource

I´ve succesfully set up a Postgres datasource on Wildfly 10.1 final. The application that uses the datasource is still under development and therefore not really in use yet.
Wildfly and postgres each are running in their own VM but on the same host machine.
About once a day, the connection apparently gets lost, and only a restart of wildfly will suffice to re-establish it.
In wildfly´s server.log I can´t find any message related to my datasource. Here is the datasource configuration from standalone.xml
<datasource jta="true" jndi-name="java:/JMPostgres" pool-name="PostgresDS" enabled="true" use-ccm="false">
<connection-url>jdbc:postgresql://10.66.1.102:5432/jm10</connection-url>
<driver-class>org.postgresql.Driver</driver-class>
<driver>postgresql-42.1.4.jar</driver>
<pool>
<min-pool-size>5</min-pool-size>
<initial-pool-size>5</initial-pool-size>
<max-pool-size>50</max-pool-size>
<prefill>true</prefill>
</pool>
<security>
<user-name>jm10</user-name>
<password>Ba4aeng$eeth0iog.ee2zohB</password>
</security>
<validation>
<valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLValidConnectionChecker"/>
<check-valid-connection-sql>SELECT 1 FROM DUAL;</check-valid-connection-sql>
<validate-on-match>true</validate-on-match>
<background-validation>true</background-validation>
<background-validation-millis>60000</background-validation-millis>
<stale-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.postgres.StaleConnectionChecker"/>
<exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLExceptionSorter"/>
</validation>
<timeout>
<allocation-retry>5</allocation-retry>
<allocation-retry-wait-millis>10000</allocation-retry-wait-millis>
</timeout>
</datasource>

JBoss and MySQL New missing/unsatisfied dependencies

I'm a Java and JBoss newbie struggling to get JBoss AS 7.0.1 working with mySQL 5.5 on my Windows 10 machine.
I get the following error when I start JBoss:-
16:19:39,480 INFO [org.jboss.as.controller] (Controller Boot Thread) Service status report
New missing/unsatisfied dependencies:
service jboss.jdbc-driver.com_mysql (missing)
This is what I've done
1 created directory jboss-as-7.0.1.Final\modules\com\mysql\main
2 Files there: module.xml and mysql-connector-java-5.1.26-bin.jar (downloaded from official mysql site)
3 Content of module.xml:
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.0" name="com.mysql" slot="main">
<resources>
<resource-root path="mysql-connector-java-5.1.39-bin.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
</dependencies>
</module>
4 Relevant parts from jboss-as-7.0.1.Final\standalone\configuration\standalone.xml:
<datasources>
<datasource jndi-name="java:/jbossdbDS" pool-name="my_pool" enabled="true" jta="true" use-java-context="true" use-ccm="true">
<connection-url>
jdbc:mysql://localhost:3306/jbossdb
</connection-url>
<driver>
com.mysql
</driver>
<security>
<user-name>
root
</user-name>
<password>
password
</password>
</security>
<timeout>
<idle-timeout-minutes>
0
</idle-timeout-minutes>
<query-timeout>
600
</query-timeout>
</timeout>
<statement>
<prepared-statement-cache-size>
100
</prepared-statement-cache-size>
<share-prepared-statements/>
</statement>
</datasource>
<datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="H2DS" enabled="true" jta="true" use-java-context="true" use-ccm="true">
<connection-url>
jdbc:h2:mem:test;DB_CLOSE_DELAY=-1
</connection-url>
<driver>
h2
</driver>
<pool>
<prefill>
false
</prefill>
<use-strict-min>
false
</use-strict-min>
<flush-strategy>
FailingConnectionOnly
</flush-strategy>
</pool>
<security>
<user-name>
sa
</user-name>
<password>
sa
</password>
</security>
</datasource>
<drivers>
<driver name="mysql" module="com.mysql"/>
<driver name="h2" module="com.h2database.h2">
<xa-datasource-class>
org.h2.jdbcx.JdbcDataSource
</xa-datasource-class>
</driver>
</drivers>
</datasources>
You have mentioned the mysql jar file name in modules as
mysql-connector-java-5.1.26-bin.jar
But in your module.xml it is incorrectly configured as
mysql-connector-java-5.1.39-bin.jar
Pls Correct your module.xml
Your driver configured in datasource with name
<driver>
com.mysql
</driver>
Update that to your correct driver name as
<driver>
mysql
</driver>
Thanks for the responses.
Turns out the mysql-connector-java-5.1.39-bin.jar file was corrupt, the java.sql.Driver file inside it was empty. Not sure how that happened!

How to get JBOSS'S datasources programmatically

I am using JBOSS 7.0 application server. I have declared my datasources in standalone.xml as follows,
<datasource jndi-name="java:jboss/datasources/ltmsfleettrackingds" pool-name="ltmsfleettrackingdspool" enabled="true" jta="true" use-java-context="true" use-ccm="true">
<connection-url>
jdbc:mysql://localhost:3306/fleettrackingdb?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8
</connection-url>
<driver>
mysqldriver
</driver>
<transaction-isolation>
TRANSACTION_READ_COMMITTED
</transaction-isolation>
<pool>
<min-pool-size>
10
</min-pool-size>
<max-pool-size>
50
</max-pool-size>
<prefill>
true
</prefill>
<use-strict-min>
false
</use-strict-min>
<flush-strategy>
FailingConnectionOnly
</flush-strategy>
</pool>
<security>
<user-name>
root
</user-name>
<password>
</password>
</security>
<validation>
<validate-on-match>
false
</validate-on-match>
<background-validation>
false
</background-validation>
<useFastFail>
false
</useFastFail>
</validation>
<statement>
<prepared-statement-cache-size>
100
</prepared-statement-cache-size>
<share-prepared-statements/>
</statement>
</datasource>
Is it possible to get the mentioned datasource jndi name declared in standalone.xml programmatically in java language.

Categories