• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

How to use datasource in Hibernate application?

July 23, 2008 //  by Krishna Srinivasan//  Leave a Comment

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.

also read:

  • WebLogic Interview Questions
  • JBoss Portal Server Development
  • Tomcat Interview Questions

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>mySQL</type-mapping>
    </metadata>
  </local-tx-datasource>
</datasource>

Put mysql-ds.xml file under jbossserverdefaultdeploy. 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>

Category: ServersTag: WildFly

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Hibernate Criteria Query API / HQL Example
Next Post: Comparing Objects in Java »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact