Prerequisites
- Working installation of JBoss AS and Eclipse. If you do not, check out Installing and Configuring JBoss AS.
- Working knowledge of:
- Java
- XML
- Eclipse
Detailed Description
A data source is a Java Naming and Directory Interface (JNDI) object used to obtain a connection from a connection pool to a database. The JBoss 4.0+ server makes use of Java Database Connectivity (JDBC) configuration files to configure the server. The JBoss application server provides data source access for J2EE? applications.
XML Configuration
To setup a datasource, an XML file that contains the database information is necessary.<datasources>
<local-tx-datasource>
<jndi-name>MyDS</jndi-name>
<connection-url>jdbc:mysql://localhost:3306/databasename</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>user</user-name>
<password>pass</password>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
<!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml -->
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>
A data source must be contained by the datasources tag. There can be multiple sources within these tags, however, it is considered best practice to only have the datasources pertinent to a single application in a file dedicated to that application. The local-tx-datasource defines a new data source and contains the information necessary to make the connection. The jndi-name tags contain the JNDI reference name. This name is what is used to access the data source within the application. The connection-url tags contain the url for the database. The jdbc:mysql:// is required for it to work correctly. localhost will be your server name, however, if the database and the application are on the same server, localhost will be fine. The last part of the url is the database name. Driver class contains the path to the necessary driver. Username and password are self explanatory.
This needs to be saved into a file named appName-environ-ds.xml, where appName is the application name and eviron is the database name. This file will be deployed directly into the JBoss AS deploy directory.
Using the Data Source
To get a connection using the data source in a Java application, the following code is needed:try {
InitialContext ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup( "java:/MyDS" );
con = ds.getConnection();
// Do some SQL and result set stuff
con.close();
}
catch (NamingException ne) {
}
catch (SQLException sqle) {
}
Note that the JNDI name that was given to the data source is used in combination with the initial context to retrieve it. It is necessary to have the java:/ before the JNDI name, otherwise it will produce a naming exception.
Benefits
There are two main benefits to data sources. It removes the database connection info from the program and allows for connection pooling. By removing this information, it makes for easy modification if it becomes necessary to change the server, username, password, etc. This also prevents from having to redeploy the application everytime a change is made. Instead, just the data source is redeployed. Obtaining a connection to the database is an expensive operation. By establishing a connection pool with a default number of connections, it reduces the strain on the system and will improve the user experience.
Connection Pooling
To add connection pooling to the data source, the number of connections need to be defined. Both the min and max may be defined, however, for more cases the minimum tag would suffice.<local-tx-datasource>
<jndi-name>MyDS</jndi-name>
<connection-url>jdbc:mysql://localhost:3306/databasename</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>user</user-name>
<password>pass</password>
<min-pool-size>5</min-pool-size>
<max-pool-size>100</max-pool-size>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
<!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml -->
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>
These two lines define the min and max connections to have pooled and available to be grabbed and used.
Using a Data Source with Hibernate
After setting up the data source, it can also be used by Hibernate. Hibernate requires much of the same information in the data source, so since we have already configured it we can just point Hibernate to it instead of entering all in again. This is specified in the Hibernate configuration file (hibernate.cfg.xml) that should be placed in the WEB-INF folder."http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.datasource">java:/MyDS</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="current_session_context_class">thread</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
There are three lines of interest here. The first,
points Hibernate to the data source that was just created. Hibernate is database independent, and thus it is required to know the language that the underlying database speaks in. Since MySQL is being used, the MySQLDialect is needed. The property with the name 'show_sql' will display the queries Hibernate has generated before it executes them. This is helpful for debugging purposes. The rest of the lines are pretty standard. In the Java code, a session factory handles all the database transactions.
Note: If there are any mapped queries or entities, they must also be referenced in this config.






Comments
Write New Comment ▼
Write New Comment
Sorry! This knol's owner(s) have blocked you from editing, making suggestions, or commenting here.