I am building a Tomcat Servlet application using Hibernate with a jta data source. My persistence.xml has the following content:
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="metadata.model" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/BitronixJTADataSource</jta-data-source>
...
</persistence>
In my ${web-app}/WebContent/META-INF/Context.xml, I have the following content:
<Context>
<Resource name="jdbc/BitronixJTADataSource" auth="Container"
type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="mysecretpassword"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/metadatadb" />
</Context>
Right after the application is started, I issue:
emf = Persistence.createEntityManagerFactory("metadata.model");
entityManager = emf.createEntityManager();
and get a
javax.naming.NameNotFoundException: Name [jdbc/BitronixJTADataSource] is not bound in this Context. Unable to find [jdbc].
Do you have any idea why this is occuring?
It appears that in the persistence.xml, the jta-data-source was supposed to have the java:comp/env/jdbc/BitronixJTADataSource value in order for it to be found in the InitialContext.
Related
I'm running into an issue where I can't get Quarkus to work with persistence.xml.
My entities are not discovered so I always get "Not an entity" errors when querying.
(Caused by: java.lang.IllegalArgumentException: Not an entity: ...)
Using Quarkus 1.13.5.Final
This might be related to Quarkus Panache Not Working with persistence.xml after 1.8
I'm migrating an existing app, I only have a single project that contains all the entities. I only use a single persistence unit.
My persistence.xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.1"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="registry-pu" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
<property name="hibernate.enable_lazy_load_no_trans" value="true"/>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.format_sql" value="false"/>
<property name="hibernate.jdbc.time_zone" value="UTC"/>
</properties>
</persistence-unit>
</persistence>
I'm only specifying these additional env variables:
QUARKUS_DATASOURCE_USERNAME=
QUARKUS_DATASOURCE_PASSWORD=
QUARKUS_DATASOURCE_JDBC_URL=
I'm also only relying on this persistence.xml because the app being migrated needs the enable_lazy_load_no_trans prop to work
Any help would be appreciated
Found I had to add <class>EntityClass</class> entries to the <persistence-unit> tag for each of my entities to have them included. Otherwise you may be able to try <exclude-unlisted-classes>false</exclude-unlisted-classes>
I'm having trouble getting a Spring/Tomcat app to resolve a variable which appears as a property of a JndiFactoryObjectName bean in the application context. Here's the relevant bean entry:
When I try to run it on the server, it comes up with this error:
Caused by: javax.naming.NameNotFoundException: Name search.url is not bound in this Context
This entry in server.xml doesn't seem to help:
There's also an entry in (as seen from Eclipse/STS)
Tomcat v6.0 Server at localhost
Catalina
localhost
ROOT.xml
<Context path="" reloadable="true" docBase="C:/myworkspace32/myAppName/WebContent">
<ResourceLink global="search.url" name="search.url" type="java.lang.String"/>
</Context>
However, this seem to have no impact.
Here are the steps to access JNDI resource from tomcat
Create jndi resource in server.xml
<Resource global="search.url" name="search.url" type="java.lang.String" />
Create the link in context.xml so that its accessible by all the web application.
<ResourceLink name="search.url" global="search.url" auth="Container" type="java.lang.String" />
Use spring bean or jee tag to inject the jndi
<bean id="searchUrl" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/search.url"/>
</bean>
you can avoid specify the environment by using jee contatiner tag as follows
<jee:jndi-lookup id="searchUrl" jndi-name="search.url" expected-type="java.lang.String" />
Follow an example of Tomcat JNDI with Spring
Spring configuration
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<jee:jndi-lookup id="yourDS" jndi-name="java:comp/env/yourDS"/>
Tomcat configuration (put this in ${catalina.home}\conf\context.xml)
<Resource
name="yourDS"
type="javax.sql.DataSource"
username="****"
password="*****"
driverClassName="com.ibm.db2.jcc.DB2Driver"
url="*******"
maxActive="8"
maxIdle="4"
/>
I am trying to inject DataSource into Servlet. Finally I've annotated datasource field with #Resource and some DBCP's BasicDataSource was injected there. But it has no configuration. No db url, no driver class, nothing. Naturally I got NullPointerException when trying to getConnection(). What am I doing wrong?
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<servlet>
<servlet-name>testServlet</servlet-name>
<servlet-class>ua.test.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>testServlet</servlet-name>
<url-pattern>/serve</url-pattern>
</servlet-mapping>
<resource-ref>
<res-ref-name>jdbc/MyDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<injection-target>
<injection-target-class>ua.test.TestServlet</injection-target-class>
<injection-target-name>dataSource</injection-target-name>
</injection-target>
</resource-ref>
</web-app>
context.xml
<?xml version='1.0' encoding='utf-8'?>
<Context>
<Resource name="jdbc/MyDB" auth="Container" type="javax.sql.DataSource"
maxActive="10" maxIdle="30" maxWait="10000"
username="tomcat" password="tomcat" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/test"/>
</Context>
Obviously no one gave a sh*t about my sorrow. So I will answer myself.
Tomcat has it's own(probably modified) copy of Apache Commons DBCP under package org.apache.tomcat.jdbc, but also for some stupid reason includes original DBCP under org.apache.commons.dbcp package. What happens when I try to inject Datasource with #Resource annotation is Tomcat instanting Datasource from original package and injecting this instance to my field. To make Tomcat to use his own copy of DBCP I had to modify my context.xml as follows:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/MyDB"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
auth="Container" type="javax.sql.DataSource"
maxActive="10" maxIdle="30" maxWait="10000"
username="tomcat" password="tomcat"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/test"/>
</Context>
Basicaly I just added this line:
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
Thanks to everyone who knew the reason but ignored this question for so long time :-)
I try to run the example project of jboss-springmvc-webapp, but I fail to configure the data source.
the error is:
JBAS014775: New missing/unsatisfied dependencies:
service jboss.jdbc-driver.mysql (missing) dependents: [service jboss.data-source.jboss/datasources/SpringQuickstartDS]
my persistence.xml is:
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="primary">
<!-- If you are running in a production environment, add a managed
data source, this example data source is just for development and testing! -->
<!-- The datasource is deployed as WEB-INF/spring-quickstart-ds.xml, you
can find it in the source at src/main/webapp/WEB-INF/spring-quickstart-ds.xml -->
<jta-data-source>java:jboss/datasources/SpringQuickstartDS</jta-data-source>
<properties>
<property name="jboss.entity.manager.factory.jndi.name" value="java:jboss/spring-quickstart/persistence" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.show_sql" value="false" />
</properties>
</persistence-unit>
</persistence>
my datasource.xml is:
<?xml version="1.0" encoding="UTF-8"?>
<datasources xmlns="http://www.jboss.org/ironjacamar/schema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.jboss.org/ironjacamar/schema http://docs.jboss.org/ironjacamar/schema/datasources_1_0.xsd">
<!-- The datasource is bound into JNDI at this location. We reference
this in META-INF/persistence.xml -->
<datasource jndi-name="java:jboss/datasources/SpringQuickstartDS"
pool-name="kitchensink-quickstart" enabled="true"
use-java-context="true">
<connection-url>jdbc:mysql://localhost:3306/testdb</connection-url>
<driver>mysql</driver>
<security>
<user-name>root</user-name>
<password></password>
</security>
</datasource>
<drivers>
<driver name="h2" module="com.h2database.h2">
<xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
</driver>
<driver name="mysql" module="com.mysql.jdbc">
<xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
</driver>
<driver name="postgresql" module="org.postgresql.jdbc">
<xa-datasource-class>org.postgresql.xa.PGXADataSource</xa-datasource-class>
</driver>
</drivers>
</datasources>
If you use the jboss-springmvc-webapp architype then it should come configured to use H2 as a datasource. I would go back to that configuration first to confirm that it works.
Then when you know everything else is working you can set up a MySQL datasource. There are 2 things you need to know. You can only point to one datasource at a time, so you must remove the other ones from the file above. It can only have the MySQL info (if that is the datasource you are using). Second, you will need to make sure you have the JDBC driver for the database that you are going to use.
Every version of JBoss has good docs on setting this up. What version of JBoss are you using, I'll post the doc page?
So i Have this problem in implementing JPA and Hibernate in Spring WS. I have configured everything correctly and according to tutorial it should work - but it is not.
Problem lies in persistance.xml, here it is how it looks:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistance_1_0.xsd"
version="1.0">
<persistence-unit name="hibernatePersistenceUnit" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.hbm2ddl.auto" value="none" />
</properties>
</persistence-unit>
</persistence>
and the exception i am getting:
Caused by: java.io.FileNotFoundException: class path resource [persistence_1_0.xsd] cannot be resolved to URL because it does not exist
at org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:177)
at org.springframework.orm.jpa.PersistenceUnitReader.validateResource(PersistenceUnitReader.java:281)
at org.springframework.orm.jpa.PersistenceUnitReader.readPersistenceUnitInfos(PersistenceUnitReader.java:108)
... 57 more
I am struggling with this since a while... Do anyone have an idea what am i missing?
You need to include appropriate jar file containing the persistence_1_0.xsd.
This is explained here.
what is the use of xsi:schemaLocation?
There was a type in the url to the .xsd file. Try using: http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd . A good way to debug these types of issues is to try to hit the url in your browser, a successful request denotes a valid link.
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="hibernatePersistenceUnit" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.hbm2ddl.auto" value="none" />
</properties>
</persistence-unit>
</persistence>
You make sure your project name does not contain any spaces, nor should the path to the project. This creates an error in Hibernate entity manager.