Do I need DataSource in JPA Hibernate project?

13,532

Solution 1

Well, you can either access the database by:

  • providing the url/driver/password/etc. information in the persistence.xml using your jpa-provider properties (in your case hibernate.connection.*) or the JPA 2.0 standardised javax.persistence.jdbc.* ones - this basically looks like the example you've posted,
  • creating a Data Source in the ApplicationServer and just referring to it in the persistence.xml (through it's JNDI name you provide during creation) which might look similar to this (without the XML schema definition for the sake of brevity) :

    <persistence>
        <persistence-unit name="SomeApp">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <jta-data-source>jdbc/myDB</jta-data-source>
        </persistence-unit>
    </persistence>
    

What you're actually doing right now (with these properties) is using the JDBC.


I would definitely go with the creation of the Data Source in the ApplicationServer rather than providing it in the properties in persistence.xml. It allows you to dynamically change the end-database, it's type, credentials, manage connection pools, etc. without even touching your descriptor.

It's also safer, as the credentials are not written in the plain file left on your server.

As a side note, please remember that the javax.persistence.jdbc.* properties are a JPA provider must requirement for the Java SE environment, but it's optional for Java EE.

Hope that helps!

Solution 2

Do I have to place mysql connector into JBOSS_HOME/standalone/deployments directory to use MySQL in my application?

Yes you need to put Mysql J/connector for use it as JDBC Driver. Your application server (JBOss, Weblogic, Glassfish, etc) doesn't provide it because depend of the RDBMS that you are using (in this case Mysql) and the version of it.

In the case of JBoss 7 the JDBC driver can be installed into the container in one of two ways: either as a deployment or as a core module. For the pros/cons of both modes an detailed explanatio you can check the following documentation: http://community.jboss.org/wiki/DataSourceConfigurationInAS7

Share:
13,532
rivasket
Author by

rivasket

Updated on July 19, 2022

Comments

  • rivasket
    rivasket almost 2 years


    I am preparing some application with usage of JPA 2.0, Hibernate as provider, MySQL 5 as database, which will be deployed on JBoss AS 7.0.2.
    I have already configured some basics in persistence.xml and I came into some kind of trouble. I have noticed that some people also defines some specific DataSource on JBoss Management Console level.
    My question is. Do I really need to worry about some DataSource or anything like that in Hibernate application?
    I thought it is important in old JDBC approach.
    In some books, where examples are shown, there is no such configuration in persistence.xml or hibernate.cfg.xml
    Do I have to place mysql connector into JBOSS_HOME/standalone/deployments directory to use MySQL in my application?

    Here is content of my persistence.xml file:

    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="2.0"
        xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
        <persistence-unit name="SomeApp">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <properties>
                <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
                <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/somedb" />
                <property name="hibernate.connection.username" value="" />
                <property name="hibernate.connection.password" value="" />
                <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
            </properties>
        </persistence-unit>
    </persistence>
    
  • rivasket
    rivasket over 12 years
    Helped a lot! Now I undersand the difference.
  • rivasket
    rivasket over 12 years
    But still I don't feel this mysql connector idea. Do I have to deploy it on JBoss or not? I have noticed that during war file deployment on JBoss I get such info between a lot of other ones: 01:06:27,794 WARN [org.hibernate.engine.jdbc.internal.JdbcServicesImpl] (MSC service thread 1-4) HHH00342:Could not obtain connection to query metadata : No suitable driver found for jdbc:mysql://localhost:3306/... Why it cannot find driver even if I have it either in JBOSS deployments directory or WEB-INF/lib?
  • Piotr Nowicki
    Piotr Nowicki over 12 years
    @rivasket take a look at DataSource Configuration In AS7. And give me a sign if it was of any help to you.
  • rivasket
    rivasket over 12 years
    Yes. This solved my problems with database connecion. Thank you very much and sorry for such delay. I had much work lately and not to much time to work on this.
  • Piotr Nowicki
    Piotr Nowicki over 12 years
    @rivasket if your problem is solved, feel free to accept one of the answers ;-)