4

i created a Java EE 5 Enterprise Application on NetBeans 7.4 including a WAR and an EJB using Weblogic 10.3.0

Im using JPA so i need to use the datasource for my persistence.xml

My problem is i'm trying to make a JDBC Datasource following this: http://docs.oracle.com/cd/E15051_01/wls/docs103/jdbc_admin/wwimages/jdbc_package_simple.gif

But Netbeans didn't add a ejb-jar.xml to the EJB, which i need for a resource reference. Oracle's documentation says it needs to be inside META-INF folder but if i add that file to that folder and then compile it is automatically deleted.

So, where is my ejb-jar.xml ? If i create it by my own what can i do to include it without being deleted after compilation ? Open the jar with WINRAR and add it manually (joke) ? Is there a easier way of doing this ?

Thanks

GabrielBB
  • 2,479
  • 1
  • 35
  • 49

2 Answers2

0

I strongly recommend you use the new Java EE 5 Annotations instead. For this example thisIsMyDataSource is the name, but you can use whatever name configured on the Server (e.g. you might have several).

@Resource
javax.sql.DataSource thisIsMyDataSource;
public Foo doSomethingNeedingADataSource() {
   Connection con = thisIsMyDataSource.getConnection();
   .........
}

Java EE 5 simplifies development by removing the requirement for that older boilerplate configuration. You can, but probably should not, use something like Apache Derby programing (perhaps through DI). There's a separation of roles in EJB. Here's an example using WebSphere. You might want to read more about JDBC.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • In your linked image the data source name being defined and mapped is my-data-source, so you'd use @Resource DataSource my-data-source ... then go to the weblogic admin console and add a Datasource named that! It's just to map to the datasource configured in your container. – Elliott Frisch Nov 14 '13 at 20:26
  • Ohh. The thing is i know how to take the connection in the web... what i really want to do is create it! i mean , create the datasource in the server automatically , not going to the console and create a jdbc datasource by myself. I want weblogic to take my datasource-jdbc.xml and create it – GabrielBB Nov 15 '13 at 12:01
  • @GabrielBB that's programmable in your Java code, but not through the weblogic datasource-jdbc.xml file contained in your ear (even if you want to use a fully self-contained, e.g. embedded, JDBC datasource - like derby or H2). – Elliott Frisch Nov 15 '13 at 16:54
  • How do you that ? like programmatically make the datasource and call it from the persistence.xml ? How would you make the JNDI ? – GabrielBB Nov 16 '13 at 04:31
  • @GabrielBB Added another paragraph to clarify my answer. – Elliott Frisch Nov 16 '13 at 04:39
  • I think you didn't understand me. That is to use a datasource already created on the server. For that, i just simply put tne name in the Persistence.xml and it takes the connection. I wanted to create what is called : JDBC Application Module. I did it with the datasource-jdbc.xml – GabrielBB Nov 17 '13 at 14:23
0

Well, i deleted my EJB (it wasn't necessary). I just created my JDBC Application Module to use it in my WAR without doing all those things in the link. Putting my datasource-jdbc.xml in the config folder (META-INF) and inserting the next code in the weblogic-application.xml was enough:

<module>
  <name>
    MyDatabase
  </name>
  <type>
    JDBC
  </type>
  <path>
    META-INF/datasource-jdbc.xml
  </path>
</module>

Then I used the JNDI "MyDatabase" in my Persistence.xml

To generate the datasource-jdbc.xml you can go to the server (a testing one), make the datasource and then go to the server folder and there is a folder (inside one of the main folders, look for it) with the name "jdbc". Inside it, you can get your generated xml.

GabrielBB
  • 2,479
  • 1
  • 35
  • 49