定义 PostGIS 连接时未找到 Oracle 连接

Not finding Oracle Connection when a PostGIS connection is defined

我在 Tomcat 8.0.20 上有一个应用程序 运行 Spring 3.1.1、Hibernate 3.6.0.Final 和 Hibernate Spatial 1.1.1。到目前为止,我们一直在使用 Oracle 11.2.0.4,但我们想迁移到 PostgreSQL 9.2-1002.jdbc4 / PostGIS 2.1.6。

我们将在不同阶段迁移数据库模式,因此在某个阶段,我们需要一些连接才能与 Oracle 一起工作,一些连接则与 PostGIS 一起工作。为了开始开发和测试它,我删除了所有 Oracle Maven 依赖项并添加了必要的依赖项以使用 Postgres,如 Hibernate Spatial 1.1.1 Tutorial 中所述,主要是:

<dependency>
    <groupId>org.hibernatespatial</groupId>
    <artifactId>hibernate-spatial-postgis</artifactId>
    <version>1.1.1</version>
</dependency>

在 Tomcat context.xml 我现在已经配置了一个连接:

<Resource accessToUnderlyingConnectionAllowed="true" auth="Container"
    autoReconnect="true" driverClassName="org.postgresql.Driver"
    logAbandoned="true" maxWait="5000" minEvictableIdleTimeMillis="60000"
    name="jdbc/dbname" numTestsPerEvictionRun="3" password="xxx"
    removeAbandoned="true" removeAbandonedTimeout="60" testOnBorrow="true"
    testWhileIdle="true" timeBetweenEvictionRunsMillis="60000"
    type="javax.sql.DataSource" url="jdbc:postgresql://servername:5432/dbname"
    username="username" validationQuery="select 1" />

这很好用。我可以先在其 Geometry 字段中插入一个空值的记录到数据库中,然后我将用该点的坐标值更新它。该字段定义为:

@Type(type = "org.hibernatespatial.GeometryUserType")
@Column(name = "geom", columnDefinition="Geometry", nullable = true) 
protected Geometry geom;

行得通,没问题。一旦我包含对 Oracle 的 Maven 依赖项,问题就会出现:

<dependency>
            <artifactId>hibernate-spatial-oracle</artifactId>
            <groupId>org.hibernatespatial</groupId>
            <version>1.1.1</version>
        </dependency>

即使我没有配置任何额外的连接,也没有更改我的项目中的任何内容以使用 Oracle,记录按预期插入到 Postgres 中,但是当涉及到更新其几何字段时,我得到了堆栈跟踪粘贴在下面,这是意外的 "Problem finding Oracle Connection" 即使连接保持不变。

org.hibernatespatial.helper.FinderException: 无法从 PreparedStatement 获取 OracleSpatial Connection 对象。

我不明白为什么 Hibernate 能够将数据插入数据库,但在尝试更新空间数据时会查找 Oracle 连接,只有当我添加了不应该的 Oracle Maven 依赖项时使用,因为我保持为 PostGIS 配置的连接。

知道我可能做错了什么吗?

提前致谢。

编辑: 似乎在启动 Tomcat 和初始化 GeometryUserType 时,使用 null 作为属性调用配置方法。因此它默认为 spatialDialect,这将是它从可用空间中找到的第一个。由于有两个(Oracle 和 PostGIS),它只会选择第一个(恰好是 Oracle)。

所以我一定是在配置中遗漏了一些东西,但我认为转到 * persistence.xml * 并声明持久性单元应该使用该方言就足够了。

<persistence-unit name="pu_name" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>${jta.datasource.prefix}datasource_name</jta-data-source>
        <class>com.myorganization.MyClass</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.dialect" value="org.hibernatespatial.postgis.PostgisDialect" />
(...)

javax.persistence.PersistenceException: org.hibernate.HibernateException: Problem finding Oracle Connection
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1389)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1317)
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1323)
    at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:965)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365)
    at com.sun.proxy.$Proxy62.flush(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
    at com.sun.proxy.$Proxy45.flush(Unknown Source)
    at com.mapflow.geotagdata.common.processing.impl.ProcessorInnerStepsImpl.executeTransactionalCommand(ProcessorInnerStepsImpl.java:1056)
    at com.mapflow.geotagdata.common.processing.impl.ProcessorInnerStepsImpl.executeTransactionalCommand(ProcessorInnerStepsImpl.java:1)
    at com.mapflow.geotagdata.common.transactions.TransactionalCommandExecutor.executeTxRequiresNew(TransactionalCommandExecutor.java:37)
    at com.mapflow.geotagdata.common.transactions.TransactionalCommandExecutor$$FastClassByCGLIB$beb81b5.invoke(<generated>)
    at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
    at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:689)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622)
    at com.mapflow.geotagdata.common.transactions.TransactionalCommandExecutor$$EnhancerByCGLIB$c535b1b.executeTxRequiresNew(<generated>)
    at com.mapflow.geotagdata.common.processing.impl.ProcessorInnerStepsImpl.synchPendingTaggedBatchToDB(ProcessorInnerStepsImpl.java:1012)
    at com.mapflow.geotagdata.common.processing.impl.ProcessorInnerStepsImpl.access[=15=](ProcessorInnerStepsImpl.java:996)
    at com.mapflow.geotagdata.common.processing.impl.ProcessorInnerStepsImpl.call(ProcessorInnerStepsImpl.java:969)
    at com.mapflow.geotagdata.common.processing.impl.ProcessorInnerStepsImpl.call(ProcessorInnerStepsImpl.java:1)
    at com.mapflow.geotagdata.common.processing.utility.RetryOnExceptionBuilder.execute(RetryOnExceptionBuilder.java:96)
    at com.mapflow.geotagdata.common.processing.impl.ProcessorInnerStepsImpl.dataTaggingPhase(ProcessorInnerStepsImpl.java:964)
    at com.mapflow.geotagdata.common.processing.impl.ProcessorOuterStepsImpl.doOuterStepLineByLineInnerSteps(ProcessorOuterStepsImpl.java:107)
    at com.mapflow.geotagdata.common.processing.impl.ProcessorImpl.process(ProcessorImpl.java:149)
    at com.mapflow.geotagdata.common.processing.impl.ProcessorImpl.processFile(ProcessorImpl.java:80)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.aop.interceptor.AsyncExecutionInterceptor.call(AsyncExecutionInterceptor.java:80)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.hibernate.HibernateException: Problem finding Oracle Connection
    at org.hibernatespatial.oracle.OracleJDBCTypeFactory.createStruct(OracleJDBCTypeFactory.java:38)
    at org.hibernatespatial.oracle.SDOGeometry.store(SDOGeometry.java:154)
    at org.hibernatespatial.oracle.SDOGeometryType.conv2DBGeometry(SDOGeometryType.java:88)
    at org.hibernatespatial.oracle.SDOGeometryType.nullSafeSet(SDOGeometryType.java:77)
    at org.hibernatespatial.GeometryUserType.nullSafeSet(GeometryUserType.java:201)
    at org.hibernate.type.CustomType.nullSafeSet(CustomType.java:140)
    at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2184)
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2559)
    at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2495)
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2822)
    at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:113)
    at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:265)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:185)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
    at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:962)
    ... 44 more
Caused by: org.hibernatespatial.helper.FinderException: Couldn't get at the OracleSpatial Connection object from the PreparedStatement.
    at org.hibernatespatial.oracle.DefaultConnectionFinder.find(DefaultConnectionFinder.java:82)
    at org.hibernatespatial.oracle.DefaultConnectionFinder.find(DefaultConnectionFinder.java:68)
    at org.hibernatespatial.oracle.DefaultConnectionFinder.find(DefaultConnectionFinder.java:68)
    at org.hibernatespatial.oracle.DefaultConnectionFinder.find(DefaultConnectionFinder.java:51)
    at org.hibernatespatial.oracle.OracleJDBCTypeFactory.createStruct(OracleJDBCTypeFactory.java:36)
    ... 61 more

如评论所述,在我看来,Hibernate 将默认使用 spatialDialect,这将是它从可用资源中找到的第一个空间方言,在那种情况下它是 Oracle,尽管 persistence.xml 中指定了什么。

我发现的第一个解决方法是在实体中注释,对于 GeometryUserType,我将使用 Postgis 方言,如下所示:

@TypeDefs({@TypeDef(name="org.hibernatespatial.GeometryUserType", 
        parameters={@org.hibernate.annotations.Parameter(name="dialect",value="org.hibernatespatial.postgis.PostgisDialect")},
        typeClass=org.hibernatespatial.GeometryUserType.class)})

这将强制 Hibernate 对该实体使用 Postgis。

第二种解决方法对我来说效果更好(我需要能够配置它并根据持久性单元和环境为一个实体使用不同的方言)是在持久性单元中使用映射文件。

<persistence-unit name="persistence_unit_name" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>jta_data_source_name</jta-data-source>
    <mapping-file>${hibernate.mappingfile}</mapping-file>
    <class>(...)</class>

在我的 pom 文件中,我将使用 Maven 配置文件和变量将该映射文件制作成我需要的文件。

<hibernate.mappingfile>oracle.hbm.xml</hibernate.mappingfile>

或:

<hibernate.mappingfile>postgis.hbm.xml</hibernate.mappingfile>

所以我会有一个 postgis.hbm.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping>
    <typedef name="org.hibernatespatial.GeometryUserType" class="org.hibernatespatial.GeometryUserType" >
        <param name="dialect">org.hibernatespatial.postgis.PostgisDialect</param>
    </typedef>
</hibernate-mapping>

还有一个 oracle.hbm.xml :

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping>
    <typedef name="org.hibernatespatial.GeometryUserType" class="org.hibernatespatial.GeometryUserType" >
        <param name="dialect">org.hibernatespatial.oracle.OracleSpatial10gDialect</param>
    </typedef>
</hibernate-mapping>

我想知道是否有更好的方法来做到这一点,但是none我这几天能找到,或者我在这里得到了回答。希望这对某人有所帮助。