JavaBeat
calling cards | international calling cards | phone card
Search JavaBeat

JAVABEAT
home
tips
articles
QnA
forums
TIPS TOPICS
Java Basics
JSP
JSF
Spring
Hibernate
EJB 3.0
JMS
Log4j
Ant
Eclipse
Netbeans
ARCHIVE
2008 | 08 07 06 05 04 03 02 01
2007 | 12 11 10 09 08
Enter email address:

Latest JavaBeat Tips Delivered
OUR NETWORK
javabeat
planetoss

How to use datasource in Hibernate application?

You may be interested...
Configure MySql database with Hibernate mappings
Collection Mapping in Hibernate : one-to-many
Comparison operators in Hibernate
NULL and NOT NULL comparison in the Hibernate API
How to use named parameters and named query in Hibernate?
Have a look at this...
JSP | JSF | Spring | Hibernate
EJB 3.0 | JMS | Log4j | Eclipse | Netbeans
Free Subscription
Enter email address:

Latest JavaBeat Tips Delivered
Author : JavaBeat
Date : Wed Jul 23rd, 2008
Topic : hibernate
Add to: Digg Add to: Del.icio.us Add to: Reddit Add to: StumbleUpon Add to: Slashdot Add to: Yahoo Add to: Google Add to: Blinklist Add to: Technorati Information

JBoss Datasource configuration in Hibernate

This article explains how to configure datasource in the JBoss application server and how to use the same datasource in the hibernate configuration file. Before looking into the hibernate configuration, we will start with creating datasource inside JBoss application server.

Create MySql datasource in JBoss

To create datasource for the MySql database inside the JBoss application server, you have to first create mysql-ds.xml file with the following entries:


<datasources>
  <local-tx-datasource>
    <jndi-name>SampleDS</jndi-name>
    <connection-url>jdbc:mysql://localhost:3306/SampleDB</connection-url>
    <driver-class>com.mysql.jdbc.Driver</driver-class>
     <min-pool-size>1>/min-pool-size>
      <max-pool-size>20>/max-pool-size>
      <user-name>root>/user-name<
      <password>root</password>
     <metadata>
       <type-mapping&g;mySQL</type-mapping>
    </metadata>
  </local-tx-datasource>
</datasource>
Put mysql-ds.xml file under jboss\server\default\deploy. JBoss application server will automatically take this file and create the datasource for you. You need not restart the server. Creating datasource is completed. Next we will look into the hibernate configuration file for maping this datasource.

Use datasource in Hibernate configuration

To map datasource in the hibernate configuration file is simple task and need not specify anything other than the datasource name. You have to include the following two lines of code in the configuration file to tell the hibernate container to use datasouce for retrieving the new connection from the databse.


<property name="dialect">
	org.hibernate.dialect.MySQLDialect
</property>	
<property name="connection.datasource">SampleDS</property>
Now we have configured datasource and mapped to the hibernate configuration file. All the connection from the hibernate will be retrieved from datasource instead of directly from the database. This approach is most widely used for the application development.

mysql-ds.xml


<?xml version="1.0" encoding="UTF-8"?>

<!-- $Id: mysql-ds.xml 63175 2007-05-21 16:26:06Z rrajesh $ -->
<!-- Datasource config for MySQL using 3.0.9 available from:
http://www.mysql.com/downloads/api-jdbc-stable.html
-->

<datasources>
<local-tx-datasource>
<jndi-name>SampleDS</jndi-name>
<connection-url>jdbc:mysql://localhost:3306/SimpleDB</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<min-pool-size>1</min-pool-size>
<max-pool-size>20</max-pool-size>
<user-name>root</user-name>
<password>root</password>-->
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
</datasource>

hibernate.cfg.xml


<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.datasource">SampleDS</property>
</session-factory>

</hibernate-configuration>

Favorites
AffiliatedAds.com
Buy movies
Access Control
Busby seo challenge contest
Sohbet
Chat
Webmaster Hosting Forum
Java Jobs
MyVideoLib
India News
Internet Advances
Sohbet
chat
Latest QnA
SCJD Tips
When we start a thread by applying start() method on it ,how does it knows that to execute run()method?
About Wrapper class in Java
How to configure weblogic 7.0 in MyEclipse?
Static Block and Static Initializer in Java

JavaBeat Website (2004-2008), India
javabeat | about us | planetoss
Copyright (2004 - 2008), JavaBeat