WildFly8 EJB3 MySQL 加载驱动程序不工作

WildFly8 EJB3 MySQL load driver don't work

您好,我使用 IDE Eclipse Luna、JavaSE8.40、WildFly8.2、MySQL5.6.23 和 Windows7 32 位。我想用@Stateful bean connect do MySQL 服务器。当我使用数据源时它工作正常(我创建数据源抛出 webconsole)。我想在没有 datasource.I 的情况下连接到数据库 加载驱动程序有问题。

这个工作正常

    public class ServerBean03 implements Rozhrani03 {
        private Context ctx;
        private DataSource dataSource;
        private Connection pripoj;
        private Statement dotaz;
        private ResultSet vysledek;
        @PostConstruct
        private void init(){
            System.out.println("Metoda Init()");
            try {
                ctx = new InitialContext();
                dataSource = (DataSource) ctx.lookup("java:jboss/jdbc/MySQLcon");           
                pripoj = dataSource.getConnection();                
                dotaz = pripoj.createStatement();           }                               
            catch (SQLException | NamingException e) {
                System.out.println("Nepodarilo se vyrobit novou databazi");
                e.printStackTrace();    }   }

这也能正常工作

public class ServerBean02 implements Rozhrani02 {
    private Connection pripoj;
    private Statement dotaz;
    private ResultSet vysledek;
    @Resource(lookup="java:jboss/jdbc/MySQLcon")  // nebo  @Resource(name="MySQLcon")
    private javax.sql.DataSource dataSource;
    @PostConstruct
    private void init(){
        System.out.println("Metoda Init()");
        try {
            pripoj = dataSource.getConnection();                        
            dotaz = pripoj.createStatement();   }                       
        catch (SQLException e) {
            System.out.println("Nepodarilo pripojit na novou databazi");
            e.printStackTrace();    }   }

这不行

public class ServerBean01 implements Rozhrani01{
    private Connection pripoj;
    private Statement dotaz;
    @PostConstruct
    private void init(){
        System.err.println("Pokusime se nacist driver");
        try {
            Class.forName("com.mysql.jdbc.Driver"); }       //  Here the Bean is crash 
        catch (ClassNotFoundException e) {
            System.err.println("Nepodarilo se nacist Driver");
            e.printStackTrace();    } 
        System.err.println("Pokusime se pripojit na databazi");
        try {
            pripoj = DriverManager.getConnection("jdbc:mysql://localhost/test", "Java2", "asdasdasd");      
            dotaz = pripoj.createStatement();   }                                                           
        catch (SQLException e) {
            System.err.println("Nepodarilo se pripojit k databazi");
            e.printStackTrace();    }   }


WildFly8.2 我是安装在ProgramFiles里的。我在文件夹模块中创建了几个文件夹。
看起来像这样:C:\Program Files\JavaEE WildFly8\modules\com\mysql\main
1) 我在那里复制 MySQL 连接器 - mysql-连接器-java-5.1.34-bin.jar - 到主文件夹
2) 我创建 module.xml - 在文件夹 main

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.3" name="com.mysql">
    <resources>
        <resource-root path="mysql-connector-java-5.1.34-bin.jar"/>
    </resources>
    <dependencies>
        <module name="javax.api"/>
        <module name="javax.transaction.api"/>
    </dependencies>
</module>

3) 我编辑 C:\Program Files\JavaEE WildFly8\standalone\configuration\standalone.xml

<subsystem xmlns="urn:jboss:domain:datasources:2.0">
        <datasources>
            <datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
                <connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE</connection-url>
                <driver>h2</driver>
                <security>
                    <user-name>sa</user-name>
                    <password>sa</password>
                </security>
            </datasource>
            <datasource jta="true" jndi-name="java:jboss/jdbc/MySQLcon" pool-name="MySQLcon" enabled="true" use-ccm="true">
                <connection-url>jdbc:mysql://localhost:3306/test</connection-url>
                <driver-class>com.mysql.jdbc.Driver</driver-class>
                <driver>mysql-connector-java-5.1.34-bin.jar_com.mysql.jdbc.Driver_5_1</driver>
                <security>
                    <user-name>Java2</user-name>
                    <password>asdasdasd</password>
                </security>
                <validation>
                    <validate-on-match>false</validate-on-match>
                    <background-validation>false</background-validation>
                </validation>
                <timeout>
                    <set-tx-query-timeout>false</set-tx-query-timeout>
                    <blocking-timeout-millis>0</blocking-timeout-millis>
                    <idle-timeout-minutes>0</idle-timeout-minutes>
                    <query-timeout>0</query-timeout>
                    <use-try-lock>0</use-try-lock>
                    <allocation-retry>0</allocation-retry>
                    <allocation-retry-wait-millis>0</allocation-retry-wait-millis>
                </timeout>
                <statement>
                    <share-prepared-statements>false</share-prepared-statements>
                </statement>
            </datasource>
            <drivers>
                <driver name="h2" module="com.h2database.h2">
                    <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
                </driver>
                <driver name="mysqlDriver" module="com.mysql">
                    <xa-datasource-class>com.mysql.jdbc.Driver</xa-datasource-class>
                    <driver-class>com.mysql.jdbc.Driver</driver-class>
                </driver>
            </drivers>
        </datasources>
    </subsystem>

我有没有做坏事?还是我忘记了什么?

您需要按照 Class Loading in WildFly

中所述,在 jboss-deployment-structure.xml 中向您的应用程序添加驱动程序模块依赖项

例如:

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
    <deployment>
        <dependencies>
            <module name="com.mysql" />
        </dependencies>
    </deployment>
</jboss-deployment-structure>