java - Apache Karaf, can't inject entity manager -
i want use entity manager container, when try access
java.lang.illegalstateexception: need active coordination
persistence.xml
<persistence-unit name="data-point" transaction-type="jta"> <jta-data-source>osgi:service/javax.sql.datasource/(osgi.jndi.service.name=dvdrental)</jta-data-source> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.postgresql82dialect"/> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.hbm2ddl.auto" value="update"/> <property name="hibernate.connection.driver_class" value="org.postgresql.driver"/> <property name="hibernate.archive.autodetection" value="class"/> </properties>
bluprint.xml
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 https://osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd" default-activation="eager" xmlns:tx="http://aries.apache.org/xmlns/transactions/v1.2.0" xmlns:jpa="http://aries.apache.org/xmlns/jpa/v2.0.0"> <jpa:enable /> <tx:enable-annotations /> <service id="filesentitymanager" ref="filesentitymanagerimpl" interface="ru.bia.karaf.dao.filesentitymanager"/> <bean id="filesentitymanagerimpl" class="ru.bia.karaf.dao.filesentitymanagerimpl"/> <bean id="testbean" class="ru.bia.karaf.web.testbean" init-method="test"> <property name="filesentitymanager" ref="filesentitymanagerimpl"/> </bean>
testbean.java
public class testbean { private filesentitymanagerimpl filesentitymanager; public void test(){ system.out.println("hey bro init"); system.out.println("filesentitymanager = " + filesentitymanager); system.out.println("filesentitymanager.getem() = " + filesentitymanager.getem()); } public filesentitymanagerimpl getfilesentitymanager() { return filesentitymanager; } public void setfilesentitymanager(filesentitymanagerimpl filesentitymanager) { this.filesentitymanager = filesentitymanager; } }
filesentitymanagerimpl.java
@osgiserviceprovider(classes = {filesentitymanager.class}) @transactional public class filesentitymanagerimpl implements filesentitymanager { @persistencecontext(unitname="data-point") entitymanager em; ... }
the entitymanager injected filesentitymanagerimpl thread local proxy of entitymanager. lifecycle bound coordination.
if access em outside of coordination error. can make sure coordination active using @transactional annotations. if not need actual transaction coordination use @transactional(txtype.supports).
you should not access entitymanager outside of object injected it.
Comments
Post a Comment