2

我有一个 Spring/Hibernate Web 应用程序(目前在 Tomcat 上运行良好),我正在尝试部署到 JBoss 7.1 AS。

应用程序都正确启动,但如果我设置 Hibernate Persistence 设置来创建表,那么我在启动时会出错。

我在 tomcat 上工作的持久性 XML 如下:

<persistence-unit name="persistenceUnit"
    transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
        <property name="hibernate.hbm2ddl.auto" value="create" />
        <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy" />
        <property name="hibernate.show_sql" value="false" />
    </properties>
</persistence-unit>

当我将此 PU 移动到 JBoss 时,我开始收到错误消息,指出它无法连接到数据库:

20:41:18,312 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (MSC service thread 1-5) HHH000231: Schema export unsuccessful: java.lang.UnsupportedOperationException: The application must supply JDBC connections

我用谷歌搜索了一个建议,通过在持久性配置中添加休眠连接详细信息来如下更新我的 PU:

<persistence-unit name="persistenceUnit"
    transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
        <property name="hibernate.hbm2ddl.auto" value="create" />
        <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy" />
        <property name="hibernate.show_sql" value="false" />
        <property name="hibernate.connection.url" value="jdbc\:mysql\://localhost\:3306/socialcv" />
        <property name="hibernate.connection.username" value="root" />
        <property name="hibernate.connection.password" value="root" />
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
    </properties>
</persistence-unit>

我已经这样做了,不再收到用户错误,但现在在应用程序启动时收到以下错误:

20:35:41,791 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (MSC service thread 1-8) HHH000231: Schema export unsuccessful: java.sql.SQLException: No suitable driver found for jdbc\:mysql\://localhost\:3306/socialcv
    at java.sql.DriverManager.getConnection(DriverManager.java:604) [rt.jar:1.7.0_03]
    at java.sql.DriverManager.getConnection(DriverManager.java:190) [rt.jar:1.7.0_03]
    at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:173) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.tool.hbm2ddl.DatabaseExporter.<init>(DatabaseExporter.java:52) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:368) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:305) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:294) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:452) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:84) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:904) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:889) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
    at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:73) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.createContainerEntityManagerFactory(PersistenceUnitServiceImpl.java:162) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.start(PersistenceUnitServiceImpl.java:85) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) [rt.jar:1.7.0_03]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) [rt.jar:1.7.0_03]
    at java.lang.Thread.run(Thread.java:722) [rt.jar:1.7.0_03]

有没有人遇到过这个或知道任何解决方案?

4

2 回答 2

4

首先:您的数据库连接 URL 看起来不正确。应该是jdbc:mysql://localhost:3306/socialcv

然后:

最好将 MySQL 驱动程序安装为模块,请参见此处:https ://community.jboss.org/wiki/DataSourceConfigurationinAS7#Installing_a_JDBC_driver_as_a_module

然后使用standalone.xml 使用该驱动程序定义数据源:

            <datasource jta="true" jndi-name="java:jboss/datasources/YourDatasourceName" pool-name="AnotherName" enabled="true" use-java-context="true" use-ccm="true">
                <connection-url>jdbc:mysql://localhost:3306/socialcv</connection-url>
                <driver>mysql</driver>
                <transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation>
                <pool>
                    <min-pool-size>10</min-pool-size>
                    <max-pool-size>100</max-pool-size>
                    <prefill>true</prefill>
                    <use-strict-min>false</use-strict-min>
                    <flush-strategy>FailingConnectionOnly</flush-strategy>
                </pool>
                <security>
                    <user-name>hehe</user-name>
                    <password>hehehe</password>
                </security>
            </datasource>
            <drivers>
                ...
                <driver name="mysql" module="com.mysql">
                    <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
                </driver>
            </drivers>

在您的 persistence.xml 中不要使用hibernate.connection.*属性,而是使用:

<non-jta-data-source>java:jboss/datasources/YourDatasourceName</non-jta-data-source>

这是初始化整个事情的事实上的方式。注意,使用时

transaction-type="JTA"

代替

transaction-type="RESOURCE_LOCAL"

你必须改为

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

它为您提供 CMT(容器管理事务)。

于 2012-04-08T21:25:26.197 回答
1

安装数据库驱动了吗?

https://community.jboss.org/wiki/DataSourceConfigurationinAS7#Installing_the_JDBC_Driver

(注意 MySQL 驱动程序的特殊问题)

最好的,安德斯

于 2012-04-08T19:54:45.287 回答