Can JBoss be configured to auto-close JDBC connections for datasources? - jboss

For JAAS authentication I have configured a datasource as follows:
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
<jndi-name>jdbc/SomeDS</jndi-name>
<connection-url>jdbc:path-to-server</connection-url>
<driver-class>interbase.interclient.Driver</driver-class>
<user-name>DBUSER</user-name>
<password>dbpass</password>
<min-pool-size>0</min-pool-size>
<metadata>
<type-mapping>Firebird</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>
Unfortunately JBoss keeps the database connection open which can cause severe performance problems on our InterBase database.
As this connection is used only by the JAAS module internally, our web application has no way to force-close the connection.
Is there a way to tell JBoss to close connections after use?

Have you tried adding <idle-timeout-minutes>? - It defines the maximum time a connection may be idle before being closed. Setting to 0 disables it. Default is 15 minutes.
See http://community.jboss.org/wiki/ConfigDataSources for details.

Related

How to enable failover for a postgresql jdbc data source

I have a data source defined in a Liberty application server for postgresql
<dataSource jdbcDriverRef="PostgreSQLDriver" ...>
<properties password="..." user="admin" serverName="server" portNumber="2020" databaseName="dbname" ssl="false"
</dataSource>
I see that I can enable failover on a jdbc connection by specifying a url like:
jdbc:postgresql://host1:port,host2:port,host3:port/dbname
Is there any way that this failover url can be provided to a application server datasource?
Yes, you can specify any key/value pair on the <properties> element and Liberty will supply it to the vendor data source class. In this case, the property would be URL="jdbc:postgresql://host1:port,host2:port,host3:port/dbname"
Also, note that in more recent versions of Liberty, there is a properties.postgresql which is specific to Postgres and better documents the available settings for it.
Here is an example,
<dataSource jdbcDriverRef="PostgreSQLDriver" ...>
<properties.postgresql password="..." user="admin" URL="jdbc:postgresql://host1:port,host2:port,host3:port/dbname" ssl="false"
</dataSource>

jboss connection pool doesn't handle errors gracefully

I'm using jboss 5.0.1 with multiple data sources configured.
It seems that on startup, if one of those is configured incorrectly, jboss throws an exception and refuses to start.
I would like to know if it's possible to configure jboss to be resilient to these kind of problems.
You can configure the connections in the xml file which holds the connection parameters/mapping. In my case, i am using a microsoft sql server as the datasource, so i have to use the mssql-ds.xml file (inside the server/deploy directory) to specify:
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
<jndi-name>myTestServer</jndi-name>
<connection-url>jdbc:sqlserver://xxx.xxx.xxx.xxx:1433;databaseName=MyDB</connection-url>
<driver-class>com.microsoft.sqlserver.jdbc.SQLServerDriver</driver-class>
<user-name>test</user-name>
<password>abc123</password>
<min-pool-size>10</min-pool-size>
<max-pool-size>25</max-pool-size>
<!-- sql to call when connection is created -->
<new-connection-sql>select getdate()</new-connection-sql>
<!-- -->
<!-- sql to call on an existing pooled connection when it is obtained from pool
This will be run before a managed connection is removed from the pool
for use by a client -->
<check-valid-connection-sql>select getdate()</check-valid-connection-sql>
</local-tx-datasource>
</datasources>
This helps control the connections in the pool, and should stop dodgy deadlocks. I'm not entirely sure whether this will test the current transaction level on the connection in question, so if you want to make absolutely sure, you can try: <check-valid-connection-sql>IF ##TRANCOUNT > 0 raiserror ('%s',18,1,'Open Transactions Discovered')</check-valid-connection-sql> This will give you an explicit SQLException to handle.
But if you are getting an error on startup, it would be very useful to see what those errors are.

Connection pooling on a Tomcat 7

I am having issues with implementing connection pooling on a tomcat7.
For some reason tomcat is trying to connect with my machines username. I have been googlin it for a while now but without a luck.
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory
(FATAL: role "caspinol" does not exist)
Cant connect to db
Log In failed: An Exception has occurred! java.lang.NullPointerException
java.lang.NullPointerException
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1549)...
My context.xml looks as follows:
<Resource auth="Container" name="jdbc/postgres" type="javax.sql.DataSource" user="biller" password="biller"
driverClassName="org.postgresql.Driver" url="jdbc:postgresql://localhost:5432" maxActive="150"
schema="biller" maxIdle="4"/>
And the web.xml:
<resource-ref>
<description>postgreSQL Datasource</description>
<res-ref-name>jdbc/postgres</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
I am using jdbc4.jar postgres driver.
I appreciate if somebody can have a look and point out what is the mistake
Thanks in advance
The URL seems to be wrong.
Try this URL :
jdbc:postgresql://localhost:5432/<db_name>
Replace the <db_name> with actual database name.
This is not the right solution here for the issue but would help somebody searching with the same issue with correct connection string.
Check for your network connection access:
Like connection string
jdbc:postgresql://[::1]:5432/<db_name>
uses IPv6,
This will break other connections like accessing wsdl urls from application that are available only via IPv4.
Please hava a look on network connection especially windows 8.

Loading properties from a file in a JBoss 6 Web Application

Can I dump a properties file somewhere in one of the JBoss 6 directories, and pick it up from the classpath?
Or even better, does anybody know the mechanism behind a configuration file like $JBOSS_HOME/server/default/deploy/jboss-logging.xml? Changes to this file seem to trigger an event, so that a running instance can process the modifications (without having to bounce the AS).
A possibility is to configure SystemPropertiesService in ./conf/jboss-service.xml.
This allows you to configure system properties in-place, or load them from a properties file:
<server>
<mbean code="org.jboss.varia.property.SystemPropertiesService"
name="jboss.util:type=Service,name=SystemProperties">
<!-- Load properties from each of the given comma seperated URLs -->
<attribute name="URLList">
http://somehost/some-location.properties,
./conf/somelocal.properties
</attribute>
<!-- Set propertuies using the properties file style. -->
<attribute name="Properties">
property1=This is the value of my property
property2=This is the value of my other property
</attribute>
</mbean>
</server>
For more details, refer to: http://docs.jboss.org/jbossas/admindevel326/html/ch10.html
They have made this even easier in JBoss EAP 6 (AS 7).
Pass Property File as Startup Parameter
This can be added within the main start up script or passed as parameter
./standalone.sh --properties=/Users/john.galt/dev/config/ds/jboss.properties
If these properties are read, they will be rendered in the server log as the first statement.
3:58:41,633 DEBUG [org.jboss.as.config] (MSC service thread 1-6) Configured system properties:
DSsettings.password = password
DSsettings.user-name = admin
DSsettings.connection-url = jdbc:oracle:fat:#activedb:1521:DEV
[Standalone] =
awt.nativeDoubleBuffering = true
NOTE: As these settings are logged in server log, ensure no clear text passwords are in the property files in production
Use passed in system properties
You could use these system properties with following syntax.
Example Usage in a data source file
<xa-datasource jndi-name="java:jboss/ds" pool-name="cPool" jta="true" enabled="true" use-ccm="true">
<xa-datasource-property name="URL">
${DSsettings.connection_url}
</xa-datasource-property>
<driver>oracle</driver>
...
<security>
<user-name>${DSsettings.user-name}</user-name>
<password>${DSsettings.password}</password>
</security>
...
</xa-datasource>
In JBoss 6 use: ./deploy/properties-service.xml
On JBoss AS7 properties-service.xml no longer exist, the below is the solution:
http://www.mastertheboss.com/jboss-server/jboss-configuration/how-to-inject-system-properties-into-jboss

JBoss Database Connection Pool

I am new to jboss and i have been asked to incorporate jboss connection pooling mechanism with an existing web application. Considering that a web application database layer is properly written i.e. all resultsets, statements and connections being closed properly when not needed, What all code changes i will have to make in my web app after i have configured the jboss datasource properly.
Can anybody please point me to a tutorial or a code sample which uses jboss datasource in a web app.
first create an xml file by name xxx-ds.xml and place this file in server/default/deploy/xxx-ds.xml
<datasources>
<local-tx-datasource>
<jndi-name>/jdbc/Exp</jndi-name>
<type-mapping>SQL</type-mapping>
<connection-url>jdbc:microsoft:sqlserver:// </connection-url>
<driver-class>com.microsoft.jdbc.sqlserver.SQLServerDriver</driver-class>
<user-name></user-name>
<password></password>
<min-pool-size>5</min-pool-size>
<max-pool-size>1000</max-pool-size>
</local-tx-datasource>
</datasources>
jboss-web.xml
<jboss-web>
<!-- <security-domain flushOnSessionInvalidation="false"/>-->
<!-- <context-root>/BSI</context-root>-->
<resource-ref>
<description>Database connection resource</description>
<res-ref-name>jdbc/Exp</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<jndi-name>java:/jdbc/Exp</jndi-name>
<res-auth>Container</res-auth>
</resource-ref>
</jboss-web>
web.xml
<resource-ref>
<description>Database connection resource</description>
<res-ref-name>jdbc/Exp</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
and now in your .java file
javax.naming.Context ctx1 = new javax.naming.InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource) ctx1.lookup("java:comp/env/jdbc/Exp");
con = ds.getConnection();
***** make sure that resource ref name should be same in all place
The pool in JBoss is all handled in the DataSource configuration. Here is the HowTo. The web app would have to do a JNDI lookup for the datasource to get the database connection rather than doing a direct JDBC URL, and then you will have pooling.
Transactions are another story, though.
EDIT: In response to your comment about how this affects the code, this is what it looks like:
String jndiPath = "java:DataSourceJNDIName"; //The exact prefix here has a lot to do with clustering, etc., but if you are using one JBoss instance standalone, this works.
Context ctx = new InitialContext();
DataSource ds = (DataSource) PortableRemoteObject.narrow(ctx.lookup(jndiPath), DataSource.class);
Connection c = ds.getConnection();
Technically speaking the PortableRemoteObject.narrow isn't necessary in a JBoss (4.2.2 anyway) single server configuration for sure, but it is more proper J2EE standard code, as general application servers don't have to return an object of the right type just for doing a Context.lookup.
The above doesn't cover the resource utilization and error handling issues. You are supposed to close that Context object when you are done with it, and of course the database connection, although JBoss will yell at you if you forget to close the database connection and the transaction ends, and close it for you.
Anyway, that Connection object is usable just as much as DriverManager.getConnection(url);
You don't have to change anything.
When you select the right kind of data source (local-tx-datasource / xa-datasource), connection handling and TX is done for you. In $JBoss/docs/examples/jca you will find templates for virtually every database, that you can just reuse.
If you are using XA, you need to configure Tx-recovery. See this posting on a how-to:
http://management-platform.blogspot.com/2008/11/transaction-recovery-in-jbossas.html (well, perhaps not a how-to in the standalone mode, but in conjunction with the Jopr source code).