开发者

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"?>
<persis开发者_JAVA百科tence 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?


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>


That's because you are using a JBoss-specific deployment file (mysql-ds.xml) for an application deployed in Tomcat. For tomcat, you'll need a different file. See this:

http://tomcat.apache.org/tomcat-6.0-doc/jndi-resources-howto.html#JDBC_Data_Sources

Not sure about the OpenEJB part, though, as I don't have experience with it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜