views:

194

answers:

5

I'm using persistence API and want to load jdbc URL from web.xml. URL should be a context parameter of servlet. I can't find how to construct EntityManagerFactory not using persistence.xml. May be I should create PersistenceUnit in servlet and set some parameters? Can you give me some short example?

Thank you

+1  A: 

I personally find it the cleanest way to define a datasource in the servlet container usng JNDI and refer to that from the persistence.xml.

This also solves one configuration issue when migrating from test-uat-prod as I can use the same datasource name on the machines.

Yeah, I know JNDI is not popular, but this works nice and it avoids having to manipulate hte web.xml which is also wrapped in the war.

Peter Tillemans
Thank you for your answer.In fact, I have to load JDBC URL from web.xml. I don't think JNDI is good solution for me. I need a very simple way. This is a just homework :)
DaTval
A: 

You can retrieve any value from the web.xml by using env-entry

  <env-entry>
    <env-entry-name>dbUrl</env-entry-name>

      <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>jdbc:url:goes:here</env-entry-value>

</env-entry>

And in java

 try {

            Context ctx = new InitialContext();
           String dbUrl= (String) ctx.lookup("java:comp/env/dbUrl");

//... create your connection


        } catch (Exception e ) {


        }
A: 

Define an init-param in your web.xml that points to your database url. For example, for MySQL, it would be something like this:

<init-param>
    <param-name>DB_URL</param-name>
    <param-value>jdbc:mysql:///MY_DB</param-value>
</init-param>

Then get this value inside your Web app (in a Servlet) using something like this:

String myDbUrl = getServletConfig().getInitParameter("DB_URL");

In JSP files you can get the value in this way:

String myDbUrl  = config.getInitParameter("DB_URL");
Bytecode Ninja
I know this, but it's not clear for me, how to make EntityManagerFactory, with this parameters :(
DaTval
+1  A: 

Whether you use a datasource or not, the JPA configuration goes in the persistence.xml, not in the web.xml. If you want to use JPA, provide a persistence.xml.

Pascal Thivent
To the point! I had the same answer in mind, but I was however a bit unsure and waited until a *Pascal Thivent* was going to post the confirming answer ;)
BalusC
Yes but my homework is to load JDBC URL, from web.xml :(
DaTval
@user357255: If it's homework, are you supposed to use JPA? As I wrote in a comment of your question, you should maybe clarify what you need to do exactly.
Pascal Thivent
quote1: "It's preferred to use JPA, but not necessary".quote2: "Database connection parameters(URL) should be in web.xml, as servlet context parameter".So I know how to get this parameters from web.xml. Now, I want to make connection using this URL.
DaTval
+2  A: 

you can use method createEntityManagerFactory(String persistenceUnitName, Map properties) of class javax.persistence.Persistence . and insert all your parameters in the map

rachvela
Yes, I wanted exactly this :)Thank you :)
DaTval
I was always using createEntityManagerFactory(String persistenceUnitName), I didn't know about another version of this method.
DaTval
+1 I totally forgot this method (theoretically, for non managed environments).
Pascal Thivent