Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAVA: how to load database url from web.xml?

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

like image 826
DaTval Avatar asked Jul 22 '26 00:07

DaTval


2 Answers

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

like image 91
rachvela Avatar answered Jul 23 '26 15:07

rachvela


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.

like image 36
Peter Tillemans Avatar answered Jul 23 '26 13:07

Peter Tillemans



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!