I assume there's a way to use an embedded Java DB instance within a standalone Felix 4.0.2 OSGi box. However I can't seem to find any clues as to how I should setup the whole thing: What implementation bundles should I use? How should I set it up?
I tried quite a few things, including getting the dedicated bundles from a glassfich 3.1.2 install, but whatever I do no persistence manager is registered by the framework to provide my app with.
My code otherwise follows all academical precepts, with a a ServiceTracker being registered to find out about EntityManagerFactory registrations, but none is found...
Here's my persistence.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
<persistence-unit name="com.julianware.toolbox.logbacktracker.pu"
transaction-type="RESOURCE_LOCAL">
<!-- mapping-file>META-INF/maintenance_orm.xml</mapping-file>
<mapping-file>META-INF/vehicle_orm.xml</mapping-file -->
<class>com.julianware.toolbox.logbacktracker.pu.LoggerEvent</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:derby:database;create=true" />
<property name="javax.persistence.jdbc.user" value="felix" />
<property name="javax.persistence.jdbc.password" value="felix" />
</properties>
</persistence-unit>
And my Activator's start() method:
public void start(final BundleContext context) throws Exception {
Filter filter = context.createFilter(String.format("(&(%s=%s)(%s=%s))",
OBJECTCLASS, EntityManagerFactory.class.getName(),
EntityManagerFactoryBuilder.JPA_UNIT_NAME, "com.julianware.toolbox.logbacktracker.pu")
);
entityManagerFactoryTracker = new ServiceTracker(context, filter, new ServiceTrackerCustomizer() {
#Override
public void removedService(ServiceReference reference, Object service) {}
#Override
public void modifiedService(ServiceReference reference, Object service) {}
#Override
public Object addingService(ServiceReference reference) {
logger.debug("Found Entity Manager Service reference.");
entityManagerFactory = (EntityManagerFactory) context.getService(reference);
return entityManagerFactory;
}
});
entityManagerFactoryTracker.open();
}
Last, I'm on JDK 7. Java DB implementation is that of the jdk install.
Does anyone know anything?
Mighty thanks.
Julian
I'm not sure, but you could try Derby from Maven Central. It should contain the correct OSGi meta data.
Related
I am trying to use a persistence unit to configure my JPA component in a camel route.
I am using EAP 6.4 and Fuse 6.3.
Right now the route is really basic just to get it working against my datasource:
private UserTransaction userTransaction;
private JpaEndpoint jpaEndpoint;
private JaxbDataFormat jaxbDataFormat;
#Override
public void configure() throws Exception {
// #formatter:off
configureErrorHandling();
configureJpa();
from("timer:startup?repeatCount=1")
.to(jpaEndpoint)
.process(exchange -> {
Exchange ex = exchange;
});
// #formatter:on
}
private void configureJpa() {
// Configure our JaxbDataFormat to point at our 'model' package
EntityManagerFactory entityManagerFactory = null;
try {
userTransaction = InitialContext.doLookup("java:jboss/UserTransaction");
entityManagerFactory = InitialContext.doLookup("java:/UtskicksbegaranOraclePU");
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
jaxbDataFormat = new JaxbDataFormat();
jaxbDataFormat.setContextPath(UtskicksbegaranRequestOracle.class.getPackage().getName());
// Configure a JtaTransactionManager by looking up the JBoss transaction manager from JNDI
JtaTransactionManager transactionManager = new JtaTransactionManager(userTransaction);
transactionManager.afterPropertiesSet();
// Configure the JPA endpoint to use the correct EntityManagerFactory and JtaTransactionManager
jpaEndpoint = new JpaEndpoint();
jpaEndpoint.setCamelContext(getContext());
jpaEndpoint.setConsumeDelete(false);
jpaEndpoint.setMaximumResults(500);
jpaEndpoint.setQuery(consumerQuery);
jpaEndpoint.setEntityType(UtskicksbegaranRequestOracle.class);
jpaEndpoint.setEntityManagerFactory(entityManagerFactory);
jpaEndpoint.setTransactionManager(transactionManager);
}
The problem here is that when I start this on my server, this line:
entityManagerFactory = InitialContext.doLookup("java:/UtskicksbegaranOraclePU"); throws javax.naming.NameNotFoundException: UtskicksbegaranOraclePU -- service jboss.naming.context.java.UtskicksbegaranOraclePU
The above code is mostly taken from a quickstart packaged with Jboss.
When I start the server without using or configuring the JPA component in my route, I can access my server and see that my persistence unit has been registered in JNDI.
Here's what my persistence.xml looks like:
<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="UtskicksbegaranOraclePU" transaction-type="JTA">
<jta-data-source>java:jboss/datasources/xa/utskicksbegaran</jta-data-source>
<class>classes</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.hbm2ddl.auto" value="validate" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.default_schema" value="icc" />
<property name="jboss.entity.manager.factory.jndi.name" value="java:/UtskicksbegaranOraclePU"/>
</properties>
</persistence-unit>
</persistence>
Basically, what I'm trying to achieve is a XA transacted fetch from my datasource which will then be distributed to different queues.
I'm using camel-spring to start the route, and this is my configuration for that:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd>
<camelContext id="camelContext_utskicksbegaran-oracle-adapter" xmlns="http://camel.apache.org/schema/spring">
<packageScan>
<package>se.sjv.integration.camel.hanterautskick</package>
</packageScan>
</camelContext>
</beans>
Is there any other way to access a persistence unit declared in persistence.xml using Spring? What am I missing?
This is finally resolved.
It had to do with load order on server startup.
For some reason, using jboss-deployment-structure.xml did not resolve this. I am now using a WEB-INF/jboss-all.xml and the file in question contains the following, which my camel integration depends on before starting:
<?xml version="1.0" encoding="UTF-8"?>
<jboss umlns="urn:jboss:1.0">
<jboss-deployment-dependencies xmlns="urn:jboss:deployment-dependencies:1.0">
<dependency name="activemq-rar.rar" />
<dependency name="ojdbc6.jar" />
</jboss-deployment-dependencies>
</jboss>
Everything runs smoothly with this.
I have been trying to make JPAKnowledgeService for about 3 days now, and I am quite close to giving up, it just seems as too much configuration and detail work for what it is/does. However,
I had this problem initially, which is gone away after I added
java.naming.factory.initial=bitronix.tm.jndi.BitronixInitialContextFactory
into my jndi.properties file, as the answer suggests. I was able to create a StatefulKnowledgeSession finally, and thought the work is over. But in drools chat, the same guy suggested that my transactions may have been being handled by Hibernate instead of Bitronix, which might make my persistence non-transactional altogether.
And I guess he is right since whenever I tried to insert an Object into the Knowledge session and call fireAllRules, I was stuck at:
executing transaction with 0 enlisted resource
followed by:
transaction timed out: a Bitronix Transaction with GTRID [3132372E302E312E310000000000AFB9D800000006], status=MARKED_ROLLBACK, 0 resource(s) enlisted (started Thu Jan 01 05:11:56 EET 1970)
After that what I changed is; I updated my 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_1_0.xsd" version="1.0">
<persistence-unit name="org.jbpm.persistence.jpa" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>java:comp/env/jdbc/jbpm</jta-data-source>
<class>org.drools.persistence.info.SessionInfo</class>
<properties>
<property name="hibernate.jndi.class" value="bitronix.tm.jndi.BitronixInitialContextFactory"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<property name="hibernate.max_fetch_depth" value="3"/>
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.BTMTransactionManagerLookup" />
</properties>
</persistence-unit>
</persistence>
added this line into my application.properties:
spring.datasource.jndi-name=java:comp/env/jdbc/jbpm
and gave a jndi name to my datasource for embedded tomcat by following these instructions.
and the error came back:
Caused by: java.lang.NullPointerException: null
at org.drools.persistence.jta.JtaTransactionManager.getStatus(JtaTransactionManager.java:273) ~[drools-persistence-jpa-6.5.0.Final.jar:6.5.0.Final]
at org.drools.persistence.jpa.AbstractPersistenceContextManager.getApplicationScopedEntityManager(AbstractPersistenceContextManager.java:78) ~[drools-persistence-jpa-6.5.0.Final.jar:6.5.0.Final]
at org.drools.persistence.jpa.JpaPersistenceContextManager.getApplicationScopedPersistenceContext(JpaPersistenceContextManager.java:55) ~[drools-persistence-jpa-6.5.0.Final.jar:6.5.0.Final]
at org.drools.persistence.SingleSessionCommandService.<init>(SingleSessionCommandService.java:103) ~[drools-persistence-jpa-6.5.0.Final.jar:6.5.0.Final]
... 43 common frames omitted
The tables related to JPAKnowledgeService are created in the database, so I guess my JNDI registration is successful, but I don't seem to be able to find Bitronix as my transaction manager since JtaTransactionManager seems as null. What am I doing wrong? I am frustrated and clueless.
Apparently, I didn't have to register my default datasource as JNDI with Tomcat, but make Bitronix directly manage it as follows:
#Bean
public PoolingDataSource setupPoolingDataSource() {
PoolingDataSource pds = new PoolingDataSource();
pds.setUniqueName("jdbc/jbpm");
pds.setClassName("bitronix.tm.resource.jdbc.lrc.LrcXADataSource");
pds.setMaxPoolSize(50);
pds.setAllowLocalTransactions(true);
pds.getDriverProperties().put("user", "username");
pds.getDriverProperties().put("password", "password");
pds.getDriverProperties().put("url", "jdbc:mysql://localhost/databaseName?useUnicode=yes&characterEncoding=UTF-8&useSSL=false");
pds.getDriverProperties().put("driverClassName", "com.mysql.jdbc.Driver");
pds.init();
return pds;
}
and deleted these stuff:
#Bean
public TomcatEmbeddedServletContainerFactory tomcatFactory() {
return new TomcatEmbeddedServletContainerFactory() {
#Override
protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(
Tomcat tomcat) {
tomcat.enableNaming();
return super.getTomcatEmbeddedServletContainer(tomcat);
}
#Override
protected void postProcessContext(Context context) {
ContextResource resource = new ContextResource();
resource.setName(name);
resource.setType(DataSource.class.getName());
resource.setProperty("url", "...");
resource.setProperty("username", "...");
resource.setProperty("password", "...");
resource.setProperty("driverClassName", "...");
resource.setProperty("factory", "org.apache.tomcat.jdbc.pool.DataSourceFactory");
context.getNamingResources().addResource(resource);
}
};
}
#Bean
public DataSource jndiDataSource() throws IllegalArgumentException, NamingException {
JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
bean.setJndiName("...");
bean.setProxyInterface(DataSource.class);
bean.setLookupOnStartup(false);
bean.afterPropertiesSet();
return (DataSource)bean.getObject();
}
everything else stays the same and it works!
I'm trying to create a layered web proyect with an EAR. I divided the Entities from the DataAccess in order to only allow DataAccess to be called by BusinessLogic.
I'm actually getting this exception:
Object: co.edu.icesi.i2trading.entities.Statetype#21397ba0 is not a known entity type.
And I found here that I need to create a persistence.xml in the EAR, but I can't really understand how.
Sharing a persistence unit across components in a .ear file
I tried placing the XML file in the Configuration Files:
And referencing the jar with <jar-files> in the persistence.xml;
<?xml version="1.0" encoding="UTF-8"?>
<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="I2TradingDataAccessPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/I2DataSource</jta-data-source>
<jar-file>../I2TradingEntities.jar</jar-file>
<jar-file>../../I2TradingEntities.jar</jar-file>
<shared-cache-mode>NONE</shared-cache-mode>
<properties>
<property name="eclipselink.target-server" value="None"/>
<!-- <property name="eclipselink.logging.level" value="ALL"/> -->
<property name="eclipselink.logging.level" value="WARNING"/>
</properties>
</persistence-unit>
</persistence>
With no success. Any idea why? I'm very new to Web Developing and this is getting to my nerves.
I'm also getting this WARNING in the persistence.xml's DESIGN tab:
I believe that the instruction jar-file interprets any path from the file that holds the persistence.xml... So, your configuration specifies that the Entities JAR are siblings of your EAR file.
The best (and easy) solution for your problem is to put the given persistence.xmlat the META-INF folder of your I2TradingEntities.jar and remove the <jar-file> instructions from it ... This will instruct the Application Server to create a PersistenceContext called "I2TradingDataAccessPU" and to detect any #Entity Class contained in that JAR.
My application is running perfectly fine in JBoss AS 5.1.0. My goal is to migrate it to Wildfly 10.
Below is my persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="ApplicationPersistenceUnit">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/MySQL_DS</jta-data-source>
<jar-file>application_db_interface.jar</jar-file>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="jboss.entity.manager.jndi.name" value="java:/ApplicationPersistenceUnit"/>
<property name="jboss.entity.manager.factory.jndi.name" value="java:/ApplicationPersistenceUnitFactory"/>
</properties>
</persistence-unit>
</persistence>
Below is my transaction manager class;
public class TransactionManager implements TransactionManagerI {
#PersistenceContext(unitName="ApplicationPersistenceUnit")
private EntityManager em;
...
}
I also try to use lookup but it still returns null;
public Session getSession() throws DAOException {
..
em = (EntityManager) new InitialContext().lookup("java:/ApplicationPersistenceUnit");
..
}
Below is the project schema;
->application.ear
->application.ear/application.war
->application.ear/db.jar
->application.ear/db.jar/META-INF/persistence.xml
I looked into other solutions and try one by one but couldn't figure out mine yet. I can provide more info if you need.
UPDATE: I check JNDI view from wildfly admin console. I don't see ApplicationPersistenceUnit there. It looks like server doesn't bind my persistence.xml file. I am now trying to figure out this problem.
I have a JAX-RS restful service which needs to access a MySQL database. I am trying to do this using CDI and a entity manager. However, when I publish the app, it appears that the incorrect persistence unit is being used (it's trying to connect on port 1527 instead of 3306).
The exception that is caught by the try/catch is:
javax.servlet.ServletException:
javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504):
org.eclipse.persistence.exceptions.DatabaseException
Internal Exception:
java.sql.SQLException:
Error in allocating a connection. Cause: Connection could not be allocated because:
java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused: connect.
Error Code: 0
Here is the restful service:
#Path("/databases")
#Stateless
public class DatabaseResource {
#PersistenceUnit(unitName = "beta.example.services")
EntityManagerFactory entityManagerFactory;
#GET
#Produces(MediaType.APPLICATION_JSON)
public Response list() {
try {
EntityManager entityManager = entityManagerFactory.createEntityManager();
Connection connection = entityManager.unwrap(java.sql.Connection.class);
...
return Response.ok().build();
} catch (SchemaCrawlerException e) {
return Response.status(500).entity(e.getMessage()).build();
}
}
}
The persistence unit (located in src/META-INF):
<?xml version="1.0" encoding="UTF-8"?>
<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="beta.example.services">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/" />
<property name="javax.persistence.jdbc.user" value="test" />
<property name="javax.persistence.jdbc.password" value="test" />
</properties>
</persistence-unit>
</persistence>
Any help would be appreciated
Ok, a persistence unit can be configured in two modes : RESOURCE_LOCAL and JTA.
Ex:
<persistence-unit name="beta.example.services" transaction-type="JTA">
VS
<persistence-unit name="beta.example.services" transaction-type="RESOURCE_LOCAL">
JTA is the default value. Properties like "javax.persistence.jdbc.*" will only be read when using "RESOURCE_LOCAL". When using JTA, the transaction manager of glassfish will be used. That means in this case you must specify JNDI name like this :
<jta-data-source>youJNDIName</jta-data-source>
What I think might be happening is that you use the default "JTA" transaction-type, but since you dont specify any jta-datasource, it might try to use the glassfish default one (wich point to derby).
Persistence unit as RESOURCE_LOCAL or JTA?
http://openejb.apache.org/jpa-concepts.html
Looking at how you use the entity manager, setting transaction-type to RESOURCE_LOCAL seems to be the solution for you.