Apache Karaf, can't inject entity manager

帅比萌擦擦* 提交于 2020-01-06 03:39:08

问题


I want to use entity manager from container, but when I try to access it I get

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 from 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;
...
}

回答1:


The EntityManager that is injected into FilesEntityManagerImpl is a thread local proxy of the EntityManager. Its lifecycle is bound to a Coordination.

If you access em outside of a Coordination you get this error. You can make sure a Coordination is active by using the @Transactional annotations. If you do not need an actual transaction but only the Coordination then use @Transactional(TxType.SUPPORTS).

You should also generally not access the EntityManager outside of the object that is injected with it.



来源:https://stackoverflow.com/questions/38327381/apache-karaf-cant-inject-entity-manager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!