Search code examples
hibernateopenejb

How should I declare my datasource in OpenEJB?


I'm trying to deploy an application in OpenEJB 3.1.4 + Tomcat 6. This application deploys OK within JBoss, but I want to get it working in OpenEJB.

Basically, I'd like to know how to configure my datasource and deploy it with an EAR for OpenEJB.

Right now:

EJBs can be found in OpenEJB's JNDI browser, but hibernate doesn't seem to find the datasource (it can't update the database schema) - from stdout:

org.hibernate.tool.hbm2ddl.SchemaExport execute INFO: Running hbm2ddl schema export
org.hibernate.tool.hbm2ddl.SchemaExport execute INFO: exporting generated schema to database
org.hibernate.tool.hbm2ddl.SchemaExport create GRAVE: Unsuccessful: create table Answer ([...])

My persistence.xml points to my datasource:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"
[...]

  <persistence-unit name="myapp">
  <provider>org.hibernate.ejb.HibernatePersistence</provider>

<jta-data-source>java:/myapp_mysql</jta-data-source>

I've tried to include a datasource file like src/main/application/mysql-ds.xml:

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

  <datasources>
    <local-tx-datasource>
      <jndi-name>myapp_mysql</jndi-name>
      <connection-url>jdbc:mysql://hostname:3306/my_app</connection-url>
      <driver-class>com.mysql.jdbc.Driver</driver-class>
      <user-name>user</user-name>
      <password>password</password>

And even another one like src/main/application/openejb.xml:

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

<openejb>
  <Connector id="myapp_mysql">
    JdbcDriver = com.mysql.jdbc.Driver
    JdbcUrl = jdbc:mysql://hostname:3306/my_app
    UserName = user
    Password = password
  </Connector>
</openejb>

But none of them seems to be loaded/taken into account by my app container. Any ideas?


Solution

  • Found it. Actually I got confused with EJB deployment and which was the real application container: OpenEJB is the application container, but is itself deployed as a webapp in Tomcat.

    First, configure your PersistenceUnit in persistence.xml:

    <?xml version="1.0" encoding="UTF-8"?>
      <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"
      [...]
    
      <persistence-unit name="myapp">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
    
      <jta-data-source>myapp_mysql</jta-data-source>
    

    Then, declare your datasource in OpenEJB config file tomcat\conf\openejb.xml

    <Resource id="myapp_mysql" type="DataSource">
      JdbcDriver  com.mysql.jdbc.Driver
      JdbcUrl   jdbc:mysql://localhost:3306/my_app
      UserName   user
      Password  password
      JtaManaged true
    </Resource>