简体   繁体   中英

Configuring MySQL DataSource using @DataSourceDefinition in JBoss AS7

I am able to configure MySQL DataSource in standalone.xml and is working fine. But I want to configure DataSource using the @DataSourceDefinition annotation.

How to configure a MySQL datasource using @DataSourceDefinition in JBoss AS7?

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 :

<?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 .

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]"]}}}

I figured it out myself.

It seems JBoss AS7 scanning process has some flaws. As per Java EE 6 spec it should scan @DataSourceDefinition annotations on any classes. But it is working fine if we put it on a class which has a @Stateless annotation.

@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

Cause param className of @DataSourceDefinition must provide DataSource implementation (see javadoc), not driver class.

For MySQL it can be:

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 .

Configure Mysql Connector jar Refer this link.

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

Data source configuration in Standalone.xml Refer this link

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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