Sunday, October 14, 2012

Data source file in JBOSS

So far, we did not discuss about database connectivity. But in this article, we are going to discussed about database connectivity in JBOSS. In jboss we have three kind of data sources.

1.<local-tx-datasource>
     Identifies a data source that uses transactions, even distributed transactions
     within the local application server, but doesn't use distributed
    transactions among multiple application servers.

2.<no-tx-datasource>

   Identifies a data source that doesn't use transactions. This option
   isn't shown in the example, but would appear in place of the
  <local-tx-datasource> tag.

3.<xa-datasource>

  Identifies a data source that uses distributed transaction among multiple
  application servers. This option isn’t shown in the example, but
  would appear in place of the <local-tx-datasource> tag.

Let's see how we implement those data sources in our applications.


With java applications we can make database connections by adding ‘jdbc’ drivers.  There programmers have to spend a lot of time in database connection management. Every time a ‘getConnection ()’ method is called a new database connection is created and it may result in a connection bottleneck. ‘Jboss’ provides an efficient way for database connection management. It is called connection pooling.
A database is referenced from an enterprise application as a data source.  Depending on whether the database is accessed from web app or ejb configuration details differ.
First of all we should create a data source file to be used by the authentication method.  Name it as akila-ds.xml and save it in server/xxx/deploy folder.  Add the below content to it. (-ds.xml is mandatory here because the server identifies a data source file by that extension)


<?xml version="1.0" encoding="UTF-8"?>
<datasources>
       <local-tx-datasource>
     <jndi-name>akilads</jndi-name>
     <connection-url>jdbc: mysql://localhost:3306/ifs_db</connection-url>
     <driver-class>com.mysql.jdbc.Driver</driver-class>
     <user-name>root</user-name>
     <password></password>
<validconnectioncheckerclassname>org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker</valid-connection-checker-class-name>
     <min-pool-size>10</min-pool-size>
     <max-pool-size>100</max-pool-size>
     <metadata>
     <type-mapping>mySQL</type-mapping>
     </metadata>
     </local-tx-datasource>
 </datasources>


Jndi name component must contain the file name without ‘-’. (akila-ds.xml -> akilads)  

Here we are going to use MySQL database. Now we created the data source file. In next article, we will see how we use this file to achieve Basic authentication login module with stored data in a database. 


1 comment:

  1. So I know that i can write up to 140 characters in this box. http://bit.ly/RgNp89

    ReplyDelete