org.hibernate.service.UnknownServiceException:请求未知服务[org.hibernate.service.jdbc.connections.spi.ConnectionProvider]

时间:2015-09-14 20:45:42

标签: hibernate java-ee jpa ejb

我有一个简单的Java EE应用程序,它使用无状态EJB和持久性对象来对表执行查找。我可以使用Hibernate 4.2和EJB 3.0将我的应用程序成功部署到WebLogic 10.3但是当我运行它时,我在我尝试使用的行和EntityManagerFactory创建EntityManager实例的代码中得到一个UnknownServiceException。 (我首先尝试简单地注入一个抛出NullPointerException的EntityManager。)

无状态EJB

@Stateless
public class OrderLocatorBean implements OrderLocator
{

  @PersistenceUnit
  private EntityManagerFactory entityManagerFactory;

  public List<Order> getOrdersByDate(Date start, Date end)
  {
    EntityManager em = this.entityManagerFactory.createEntityManager();
    TypedQuery<Order> queryByDate = em.createNamedQuery("findByDate", Order.class);
    queryByDate.setParameter("startdate", start);
    queryByDate.setParameter("enddate", end);
    List<Order> results = queryByDate.getResultList();
    em.close();

    return results;
  }
}

EJB打包在order.jar中,它包含在ear文件中。这是 ejb-jar.xml 文件。

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar
  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/ejb-jar_3_0.xsd"
  version="3.0"
>

  <enterprise-beans>

    <session>

      <ejb-name>OrderLocatorBean</ejb-name>
      <transaction-type>Container</transaction-type>

    </session>

  </enterprise-beans>

</ejb-jar>

这是我的 persistence.xml 文件打包在META-INF目录中的order.jar中:

<?xml version="1.0"?>

<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="Order" transaction-type="JTA">

    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>contexte-datasource</jta-data-source>
    <class>order.Order</class>

    <properties>
      <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
    </properties>

  </persistence-unit>

</persistence>

以下是EJB抛出的异常(最后一部分):

Caused by: org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.service.jdbc.connections.spi.ConnectionProvider]
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:151)
    at org.hibernate.internal.AbstractSessionImpl.getJdbcConnectionAccess(AbstractSessionImpl.java:262)
    at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.<init>(JdbcCoordinatorImpl.java:106)
    at org.hibernate.engine.transaction.internal.TransactionCoordinatorImpl.<init>(TransactionCoordinatorImpl.java:90)
    at org.hibernate.internal.SessionImpl.<init>(SessionImpl.java:254)
    at org.hibernate.internal.SessionFactoryImpl$SessionBuilderImpl.openSession(SessionFactoryImpl.java:1618)
    at org.hibernate.ejb.EntityManagerImpl.getRawSession(EntityManagerImpl.java:121)
    at org.hibernate.ejb.EntityManagerImpl.getSession(EntityManagerImpl.java:97)
    at org.hibernate.ejb.AbstractEntityManagerImpl.joinTransaction(AbstractEntityManagerImpl.java:1207)
    at org.hibernate.ejb.AbstractEntityManagerImpl.postInit(AbstractEntityManagerImpl.java:178)
    at org.hibernate.ejb.EntityManagerImpl.<init>(EntityManagerImpl.java:89)
    at org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:193)
    at org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:188)
    at sun.reflect.GeneratedMethodAccessor589.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at weblogic.deployment.EntityManagerFactoryProxyImpl.invoke(EntityManagerFactoryProxyImpl.java:96)
    at $Proxy238.createEntityManager(Unknown Source)
    at order.OrderLocatorBean.getOrdersByDate(OrderLocatorBean.java:40)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.bea.core.repackaged.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:310)
    at com.bea.core.repackaged.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
    at com.bea.core.repackaged.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
    at com.bea.core.repackaged.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:131)
    at com.bea.core.repackaged.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:119)
    at com.bea.core.repackaged.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
    at com.oracle.pitchfork.spi.MethodInvocationVisitorImpl.visit(MethodInvocationVisitorImpl.java:34)
    at weblogic.ejb.container.injection.EnvironmentInterceptorCallbackImpl.callback(EnvironmentInterceptorCallbackImpl.java:54)
    at com.oracle.pitchfork.spi.EnvironmentInterceptor.invoke(EnvironmentInterceptor.java:42)
    at com.bea.core.repackaged.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
    at com.bea.core.repackaged.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
    at com.bea.core.repackaged.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
    at com.bea.core.repackaged.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:131)
    at com.bea.core.repackaged.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:119)
    at com.bea.core.repackaged.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
    at com.bea.core.repackaged.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    at $Proxy244.getOrdersByDate(Unknown Source)
    at order.OrderLocatorBean_43jnw0_OrderLocatorImpl.__WL_invoke(Unknown Source)
    at weblogic.ejb.container.internal.SessionLocalMethodInvoker.invoke(SessionLocalMethodInvoker.java:39)
    ... 58 more

1 个答案:

答案 0 :(得分:2)

我能够通过设置hibernate配置属性hibernate.transaction.factory_class来解决这个问题。请参阅此处的文档:

http://docs.jboss.org/hibernate/orm/4.2/manual/en-US/html_single/#transactions-demarcation-jta http://docs.jboss.org/hibernate/orm/4.2/manual/en-US/html_single/#configuration-optional-dialects

我查看了hibernate源代码,虽然我无法确切地指出确切原因,但我可以看到一些初始化失败了。看起来像weblogic(可能还有其他)服务器,hibernate需要知道如何创建JTA事务。我的更新文件如下。

<强>的persistence.xml

<?xml version="1.0"?>

<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="Order" transaction-type="JTA">

    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>contexte-datasource</jta-data-source>
    <class>order.Order</class>

    <properties>
      <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
      <property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory"/>
    </properties>

  </persistence-unit>

</persistence>

无状态EJB

@Stateless
public class OrderLocatorBean implements OrderLocator
{

  @PersistenceContext
  private EntityManager entityManager;

  public List<Order> getOrdersByDate(Date start, Date end)
  {
    TypedQuery<Order> queryByDate = this.entityManager.createNamedQuery("findByDate", Order.class);
    queryByDate.setParameter("startdate", start);
    queryByDate.setParameter("enddate", end);
    List<Order> results = queryByDate.getResultList();

    return results;
  }
}