Wildfly 9 Hibernate 使用 H2 数据库而不是 MS SQL 服务器

Wildfly 9 Hibernate using H2 database instead of MS SQL Server

我的 Java EE 应用程序工作正常,但突然拒绝对数据库执行任何操作。在重新部署时,我得到了以下堆栈跟踪:

12:08:54,001 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (ServerService Thread Pool -- 69) Table "USERS" not found; SQL statement:
select userentity0_.id as id1_7_, userentity0_.password as password2_7_, userentity0_.username as username3_7_ from users userentity0_ [42102-173]
(...)
Caused by: org.h2.jdbc.JdbcSQLException: Table "USERS" not found; SQL statement:
select userentity0_.id as id1_7_, userentity0_.password as password2_7_, userentity0_.username as username3_7_ from users userentity0_ [42102-173]
        at org.h2.message.DbException.getJdbcSQLException(DbException.java:331)
(...)

我注意到这里有一件奇怪的事情。从堆栈跟踪看来,它正在使用 H2 数据库驱动程序(或其他东西),而不是 Microsoft SQL Server,就像以前一样.我不记得最近对数据库进行了任何更改,它只是突然冒出来。

我发现这里已经有人问过类似的问题: Wildfly mysql instead of h2 但是那里发布的答案并不令我满意 - 我希望它在没有数据源的情况下工作,只需将所有数据库配置存储在 persistence.xml 中并使其保持应用程序服务器独立(尽可能)。

我的 persistence.xml 文件:

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
             http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
             version="2.1">

    <persistence-unit name="manager1" transaction-type="JTA">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <!-- some classes in <class> tags -->        
        <properties>
            <property name="hibernate.connection.driver_class" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
            <property name="hibernate.connection.url" value="jdbc:sqlserver://<my Azure database server>:1433;database=<db name>;user=<username>;password=<pass>;encrypt=true;hostNameInCertificate=*.database.windows.net;loginTimeout=30;"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServer2012Dialect"/>
            <!--<property name="hibernate.hbm2ddl.auto" value="update"/>-->
        </properties>
    </persistence-unit>
</persistence>

我应该更改什么才能使其再次与 MS SQL 服务器数据库一起工作?

您需要define the MS SQL data source in the container. Here's an example 添加 PosgreSQL 驱动程序和数据源。

然后在您的 persistence.xml 中定义 JTA 数据源:

<jta-data-source>java:jboss/datasources/MSSqlDS</jta-data-source>

这是必需的,因为您希望容器管理您的事务。据我所知,您不能使用 JTA 定义连接参数。

您看到使用 H2 的原因是默认情况下它使用 ExampleDS 这是默认数据源。您可以将自己的数据源设置为默认数据源,并从您的 persistence.xml 中删除 <jta-data-source/>

/substem=ee/service=default-bindings:write-attribute(name=datasource, value="java:jboss/datasources/MSSqlDS")

这将设置默认数据源,java:comp/DefaultDataSource,成为您定义的数据源。