简体   繁体   English

在JBoss AS7中使用@DataSourceDefinition配置MySQL数据源

[英]Configuring MySQL DataSource using @DataSourceDefinition in JBoss AS7

I am able to configure MySQL DataSource in standalone.xml and is working fine. 我能够在standalone.xml中配置MySQL DataSource,并且工作正常。 But I want to configure DataSource using the @DataSourceDefinition annotation. 但是我想使用@DataSourceDefinition注释配置DataSource。

How to configure a MySQL datasource using @DataSourceDefinition in JBoss AS7? 如何在JBoss AS7中使用@DataSourceDefinition配置MySQL数据源?

What I have already tried is: 我已经尝试过的是:

@DataSourceDefinition(
        className = "com.mysql.jdbc.Driver",
        name = "java:global/jdbc/MyDS",
        serverName="localhost",
        portNumber=3306,
        user = "root",
        password = "admin",
        databaseName = "test"
)
@Startup
public class DBConfig {
}

together with this persistence.xml : 连同这个persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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">
    <persistence-unit name="javaee6-app" transaction-type="JTA">
        <jta-data-source>java:global/jdbc/MyDS</jta-data-source>
        <properties>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
            <property name="hibernate.show_sql" value="true"/>
        </properties>
    </persistence-unit>
</persistence>

I have the mysql connector jar file in WEB-INF/lib . 我在WEB-INF/lib有mysql连接器jar文件。

But when I deploy the application I am getting this error: 但是,当我部署应用程序时,出现此错误:

(DeploymentScanner-threads - 2) {"JBAS014653: Composite operation failed and was rolled back. Steps that failed:" => {"Operation step-2" => {"JBAS014771: Services with missing/unavailable dependencies" => ["jboss.persistenceunit.\\"javaee6-app.war#javaee6-app\\"jboss.naming.context.java.global.jdbc.MyDSMissing[jboss.persistenceunit.\\"javaee6-app.war#javaee6-app\\"jboss.naming.context.java.global.jdbc.MyDS]"]}}} (DeploymentScanner-threads-2){“ JBAS014653:组合操作失败并被回滚。失败的步骤:” => {“ Operation step-2” => {“ JBAS014771:缺少/不可用依赖项的服务” => [“ jboss.persistenceunit。\\“ javaee6-app.war#javaee6-app \\” jboss.naming.context.java.global.jdbc.MyDSMissing [jboss.persistenceunit。\\“ javaee6-app.war#javaee6-app \\” jboss。 naming.context.java.global.jdbc.MyDS]“]}}}

I figured it out myself. 我自己弄清楚了。

It seems JBoss AS7 scanning process has some flaws. 看来JBoss AS7的扫描过程存在一些缺陷。 As per Java EE 6 spec it should scan @DataSourceDefinition annotations on any classes. 根据Java EE 6规范,它应扫描任何类上的@DataSourceDefinition批注。 But it is working fine if we put it on a class which has a @Stateless annotation. 但是,如果我们将其放在具有@Stateless批注的类上,则效果很好。

@DataSourceDefinition(
        className = "com.mysql.jdbc.Driver",
        name = "java:global/jdbc/MyDS",
        serverName="localhost",
        portNumber=3306,
        user = "root",
        password = "admin",
        databaseName = "test"
)
@Stateless
public class DBConfig {
    public void test() { //there should be atleast one method, so this dummy
    }
}

Also the next problem that you can have is: 另外,您可能遇到的下一个问题是:

java.lang.ClassCastException: com.mysql.jdbc.Driver cannot be cast to javax.sql.DataSource java.lang.ClassCastException:com.mysql.jdbc.Driver无法转换为javax.sql.DataSource

Cause param className of @DataSourceDefinition must provide DataSource implementation (see javadoc), not driver class. 原因@DataSourceDefinition param className必须提供DataSource实现(请参见javadoc),而不是驱动程序类。

For MySQL it can be: 对于MySQL,它可以是:

className = "com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
className = "com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource"
className = "com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"    // XA transaction

One suggestion is that configure data source name in standalone.xml or domain.xml in jboss as 7. and configure mysql connector jar in jboss module.And try . 一种建议是在jboss中的standalone.xml或domain.xml中将数据源名称配置为7,并在jboss模块中配置mysql连接器jar。然后尝试。

Configure Mysql Connector jar Refer this link. 配置Mysql Connector jar请参考此链接。

http://www.mastertheboss.com/jboss-datasource/how-to-configure-a-datasource-with-jboss-7 http://www.mastertheboss.com/jboss-datasource/how-to-configure-a-datasource-with-jboss-7

Data source configuration in Standalone.xml Refer this link Standalone.xml中的数据源配置请参阅此链接

https://docs.jboss.org/author/display/AS71/DataSource+configuration https://docs.jboss.org/author/display/AS71/DataSource+configuration

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM