I have trouble binding local and remote versions of service beans to the Global JNDI for Firebird database. To check whether I'm missing something I also setup a test configuration for SQL Server which works just fine. I'm using jaybird-2.1.6.jar. Something is incomplete or wrong in my Firebird JPA setup.
When I use d20pid/D20PIDServiceBean/local to access the local service bean I get an error saying:
Could not get the JNDI resource with the following JNDI name: d20pid/D20PIDServiceBean/local
Understandably so since it hasn't been bound.
Any help would be greatly appreciated! Thank you!
Persistence.xml part for firebird:
<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_2_0.xsd"
version="2.0">
<persistence-unit name="D20-PID-DS">
<jta-data-source>java:/D20-PID-DS</jta-data-source>
<class>edu.msu.its.d20pid.ejb.entity.ProvStudent</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.FirebirdDialect" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
Datasource file firebird details (intentionally left out credentials)
<local-tx-datasource>
<jndi-name>D20-PID-DS</jndi-name>
<connection-url>jdbc:firebirdsql:idcard.dev.ais.msu.edu:/home2/d20/idcard.db</connection-url>
<driver-class>org.firebirdsql.jdbc.FBDriver</driver-class>
<user-name></user-name>
<password></password>
<idle-timeout-minutes>15</idle-timeout-minutes>
<check-valid-connection-sql>SELECT CAST(1 as INTEGER) FROM rdb$database</check-valid-connection-sql>
<track-statements>false</track-statements>
<prepared-statement-cache-size>0</prepared-statement-cache-size>
<metadata>
<type-mapping>Firebird</type-mapping>
</metadata>
</local-tx-datasource>
server.log on EAR deployment; local and remote for D6501ServiceBean (SQL Server) are bound but not for D20PIDServiceBean (Firebird)
17:10:23,318 INFO [SessionSpecContainer] Starting boss.j2ee:ear=d20pid.ear,jar=d20pid-ejb3.jar,name=D20PIDServiceBean,service=EJB3
17:10:23,322 INFO [EJBContainer] STARTED EJB: edu.msu.its.d20pid.ejb.servicebean.D20PIDServiceBean ejbName: D20PIDServiceBean
17:10:23,324 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
17:10:23,327 INFO [SessionSpecContainer] Starting jboss.j2ee:ear=d20pid.ear,jar=d20pid-ejb3.jar,name=D6501ServiceBean,service=EJB3
17:10:23,329 INFO [EJBContainer] STARTED EJB: edu.msu.its.d20pid.ejb.servicebean.D6501ServiceBean ejbName: D6501ServiceBean
17:10:23,357 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
d20pid/D6501ServiceBean/remote - EJB3.x Default Remote Business Interface
d20pid/D6501ServiceBean/remote-edu.msu.its.d20pid.ejb.service.remote.D6501ServiceRemote - EJB3.x Remote Business Interface
d20pid/D6501ServiceBean/local - EJB3.x Default Local Business Interface
d20pid/D6501ServiceBean/local-edu.msu.its.d20pid.ejb.service.local.D6501ServiceLocal - EJB3.x Local Business Interface
My code is working now. I hadn't annotated the local and remote beans with #Local and #Remote. That fixed it.
Related
How does spring know which connection pool to use?
As is known ,you tell the spring framework a persistence-unit name,and annotate the entity manager with #PersistenceContext,and with Persistence.xml configured.Spring does every thing for you.
I am very confused about the spring annotation "#PersitenceContext" above the entityManager field.
My persistence.xml is as below :
<persistence-unit name="hibernate.recommendation_report.jpa">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:#192.168.113.226:11521:BOSS" />
<property name="javax.persistence.jdbc.user" value="xxxx" />
<property name="javax.persistence.jdbc.password" value="xxxx" />
</properties>
</persistence-unit>
My tomcat server and my webapp work well when and after for a short peoriod of time after the starting up of tomcat server.But hours later,the server reports a sqlexception "Connection already closed".
Is this the problem of misusing db connection pool? How do the spring framework choose a c3p0 or DBCP ? How would I specify the connection pool? Or is the tomcat uses the default DBCP as the connection pool?
U can make your tomcat server or other app server provide the JNDI datasource. So that, the your server container's self-contained connection pool can take good care of your the database connection/session.
Tomcat in your case, you specify the JNDI datasource in the $TOMCAT_HOME/conf/context.xml or server.xml:
<Resource name="jdbc/sample" auth="Container"
type="com.mchange.v2.c3p0.ComboPooledDataSource"
username=...
password=...
url=...
driverClassName=...
/>
the type attribute tells the tomcat which connection pool to use.
By default tomcat6 uses DBCP with type of "java.sql.DataSource".
Make sure to use the JNDI reference in your persistence.xml:
<persistence version="2.1" ....>
<persistence-unit name="hibernate.recommendation_report.jpa">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<non-jta-data-source>java:comp/env/jdbc/sample</non-jta-data-source>
</persistence-unit>
</persistence>
Or use spring xml to config the datasource bean to inject into the your entityManagerFactory.Please refer to here.
<jee:jndi-lookup id="dataSource" jndi-name="java:sample"/>
See other JNDI resource attributes available for DBCP.
Note: the "java:comp/env/" prefix in persistence.xml data-source is very import.Without it, Spring will not look for the pool provided by your application server to fetch datasource but just use the attribute to construct a simple datasource.
Note: tomcat8 itself provides a even better pool.If you upgrade to tomcat8.
I am trying to set up a persistence unit in JEE 6 on JBoss AS 6.
The injected EntityManager is always null. I've fiddled around with the configuration quite a bit but can't get anything to work. I've tried JTA/LOCAL_RESOURCE, I've tried using a unit name in my code the same as in my persistence.xml, but also with the war name prepended, as the log suggests the JNDI name might be that.
I have a mysql-ds.xml file as follows:
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
<jndi-name>myconnection</jndi-name>
<connection-url>jdbc:mysql://localhost:3306/cog1</connection-url>
<driver-class>org.gjt.mm.mysql.Driver</driver-class>
<user-name>bw</user-name>
<password></password>
<min-pool-size>2</min-pool-size>
<max-pool-size>20</max-pool-size>
<idle-timeout-minutes>5</idle-timeout-minutes>
<exception-sorter-class-name>com.mysql.jdbc.integration.jboss.ExtendedMysqlExceptionSorter</exception-sorter-class-name>
<valid-connection-checker-class-name>com.mysql.jdbc.integration.jboss.MysqlValidConnectionChecker</valid-connection-checker-class-name>
</local-tx-datasource>
</datasources>
I have a persistence.xml as follows:
<?xml version="1.0" encoding="UTF-8"?>
<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_2_0.xsd"
version="2.0">
<persistence-unit name="myunit" transaction-type="RESOURCE_LOCAL">
<jta-data-source>java:/myconnection</jta-data-source>
<non-jta-data-source>java:/myconnection</non-jta-data-source>
<class>com.cognitura.simulation.dao.model.History</class>
<properties>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
<property name="eclipselink.logging.level" value="INFO" />
</properties>
</persistence-unit>
</persistence>
And I have a bean with the following code in it:
#Stateful
#LocalBean
public class HistoryEJB {
#PersistenceContext(unitName = "myunit")
private EntityManager em;
#EJB
private RealityElement el;
I can see that the RealityElement el is getting injected fine, so I think it's definitely being container-managed?
Grateful for any ideas!
EDIT: The logs seem to be fine for setting up the 'myunit' unit. There is a warning, but I read that it's not important?
01:28:06,276 INFO [org.jboss.jpa.deployment.PersistenceUnitDeployment] Starting persistence unit persistence.unit:unitN
ame=cog1.war#myunit
01:28:06,278 INFO [org.hibernate.ejb.Ejb3Configuration] Processing PersistenceUnitInfo [
name: myunit
...]
01:28:06,305 WARN [org.hibernate.ejb.Ejb3Configuration] Persistence provider caller does not implement the EJB3 spec co
rrectly.PersistenceUnitInfo.getNewTempClassLoader() is null.
Seems happy here:
01:28:06,965 INFO [org.hibernate.impl.SessionFactoryObjectFactory] Bound factory to JNDI name: persistence.unit:unitNam
e=cog1.war#myunit
Could it be the jndi name? Try making the following changes in datasource and persistence files
<jndi-name>java:jboss/datasources/employeedb</jndi-name>
<jta-data-source>java:jboss/datasources/employeedb</jta-data-source>
I have Vaadin project with JPA 2.1 + EclipseLink + MySQL.
In eclipse in project settings I set connection to base and drivers correctly. Without problem I "generate Entities from Tables", but persistence.xml haven't info about driver, connection, login, pass to DB.
My auto generate persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="RadiologicalWarehouse">
<class>pl.intibs.rw.entitites.Message</class>
<class>pl.intibs.rw.entitites.Part</class>
<class>pl.intibs.rw.entitites.Role</class>
<class>pl.intibs.rw.entitites.Sample</class>
<class>pl.intibs.rw.entitites.SampleHistory</class>
<class>pl.intibs.rw.entitites.User</class>
</persistence-unit>
</persistence>
If I run my application with simple query, I get of course exception:
javax.servlet.ServletException: com.vaadin.server.ServiceException: javax.persistence.PersistenceException: Exception [EclipseLink-4021] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException
Exception Description: Unable to acquire a connection from driver [null], user [null] and URL [null]. Verify that you have set the expected driver class and URL. Check your login, persistence.xml or sessions.xml resource. The jdbc.driver property should be set to a class that is compatible with your database platform
com.vaadin.server.VaadinServlet.service(VaadinServlet.java:239)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
If JPA has defined the connection in the project, why does not generate this persistence automatically?
Screenshot settings JPA: http://i.imgur.com/T2oEdHA.png
Based on what I understand you have set parameters for the Jpa plugin to eclipse and this is much different to set the parameters to persistence provider. Firstly you have to decide what persitent provide use and specify it in the persistence.xml together with the others paramenters.
This is an example of persistence.xml with eclipseLink:
<persistence>
<persistence-unit name="myUnit">
<provider>yourProvider</provider>
<class>it.myCompany.domain.MyFirstClass</class>
<class>it.myCompany.domain.MySecondClassClass</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="${driverClass}" />
<property name="javax.persistence.jdbc.url" value="${connectionURL}" />
<property name="javax.persistence.jdbc.user" value="${username}" />
<property name="javax.persistence.jdbc.password" value="${password}" />
<property name="eclipselink.logging.level" value="info"/>
</properties>
<shared-cache-mode>NONE</shared-cache-mode>
</persistence-unit>
</persistence>
I'm writing a small EJB3 application deployed on glassfish 4 and using the derbyDB that comes with glassfish.
To make testing easy I have declared one of my EJBs as #Webservice so I can trigger a method with the testers provided by glassfish. So far so good.
The callstack is like this:
DailyReportingJob( #Stateless and #WebService, EntryPoint for my test)
--> VerfahrensArchivService (#Stateless and #Transactional(value = TxType.REQUIRES_NEW) )
my persistence.xml is like this:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="zvagent">
<jta-data-source>jdbc/zvagentdb</jta-data-source>
<class>de.kevinfleischer.zvagent.verfahren.Verfahren</class>
<properties>
<property name="eclipselink.logging.level" value="FINE"/>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
<property name="eclipselink.deploy-on-startup" value="true"/>
</properties>
</persistence-unit>
</persistence>
I've configured a connection pool in glassfish of ressource type javax.sql.DataSource and a jdbc ressource with the name jdbc/zvagentdb that points to this pool.
When I trigger the webservice I see my app working. But when I try to call the VerfahrensArchivService - which should store data to the DB - the following warning shows up. In the webinterface of the webservice tester I see further an InvocationTargetException, telling me the call to "doPost" failed.
WARNING: javax.ejb.EJBException: java.lang.IllegalStateException: Transaction is not active in the current thread.
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2016)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1979)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:220)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)
at com.sun.proxy.$Proxy455.storeAndUpdateVerfahren(Unknown Source)
at de.kevinfleischer.zvagent.archiv.__EJB31_Generated__VerfahrensArchivService__Intf____Bean__.storeAndUpdateVerfahren(Unknown Source)
at de.kevinfleischer.zvagent.job.DailyReportingJob.executeJob(DailyReportingJob.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
The DB is empty afterwards. No Data was stored.
(I have JUnit tests, that work completely local without appserver, that show, that the code would store data. So its a transaction problem, not a problem in the business code.)
I finally found that the configuration was no problem. The packaging was rubbish and the errors I got only follow up errors.
I am newbie with JPA. I'm trying to run some sample code using JPA but I get the following exception:
javax.persistence.PersistenceException: No Persistence provider for EntityManager named MyJPAApplicationPU
I put my exception message here,
INFO: Could not find any META-INF/persistence.xml file in the classpath
javax.persistence.PersistenceException: No Persistence provider for EntityManager named MyJPAApplicationPU
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:33)
at com.myJpa.TestJPA.setUp(TestJPA.java:30)
at com.myJpa.TestJPA.main(TestJPA.java:72)
Any help would be appreciated.
Well, the error is self explaining, you need to provide a META-INF/persistence.xml to use JPA. This file is used to define a "persistence unit". From the JPA 1.0 specification:
6.2.1 persistence.xml file
A persistence.xml file defines a
persistence unit. It may be used to
specify managed persistence classes
included in the persistence unit,
object/relational mapping information
for those classes, and other
configuration information for the
persistence unit and for the entity
manager(s) and entity manager factory
for the persistence unit. The
persistence.xml file is located in
the META-INF directory of the root
of the persistence unit. This
information may be defined by
containment or by reference, as
described below.
Here is a sample persistence.xml for a Java SE environment (using Hibernate as JPA provider):
<?xml version="1.0" encoding="UTF-8"?>
<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="MyJPAApplicationPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.mycompany.Foo</class>
<class>com.mycompany.Bar</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.connection.provider_class" value="org.hibernate.connection.DriverManagerConnectionProvider" />
<property name="hibernate.connection.url" value="jdbc:hsqldb:mem:unit-testing-jpa"/>
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.connection.username" value="sa"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
Some comments about the above file:
when running in a Java SE environment, you cannot rely on JTA and the transaction type must be RESOURCE_LOCAL (which is actually the default in a Java SE environment but specifying it make it more clear).
when running in a Java SE environment, you cannot use a JDNI data source and the provider will obtain connections directly from the JDBC driver so you must pass the relevant informations to the provider (driver class name, connection url, user, password). With JPA 1.0 the properties used to pass these metadata are provider specific.
To insure the portability of a Java SE application, it is necessary to explicitly list the managed persistence classes that are included in the persistence unit.
For JPA to work, you need META-INF/persistence.xml. I will assume this is a web-app, so this folder has to be in WEB-INF/classes/.
The persistence.xml file would look like this:
<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="MyJPAApplicationPU" transaction-type="RESOURCE_LOCAL">
</persistence-unit>
</persistence>
EntityManagerFactory emf = Persistence.createEntityManagerFactory("<JDBC connection>");
Check the correct JDBC Connection.