簡體   English   中英

無法通過JBoss檢測JPA使用的數據庫

[英]Can't detect Database used by JPA with JBoss

可能是缺乏知識,但是我無法檢測到我的應用程序正在使用哪個數據庫。 來自JBoss AS的standalone.xml中的數據源和驅動程序如下所示:

 <subsystem xmlns="urn:jboss:domain:datasources:1.0"> <datasources> <datasource jta="true" jndi-name="java:/MySqlDS" pool-name="MySqlDS_Pool" enabled="true" use-java-context="true" use-ccm="true"> <connection-url>jdbc:mysql://localhost:3306/studadmin</connection-url> <driver>com.mysql</driver> <security> <user-name>sa</user-name> <password>sa</password> </security> <timeout> <idle-timeout-minutes>0</idle-timeout-minutes> <query-timeout>600</query-timeout> </timeout> <statement> <prepared-statement-cache-size>100</prepared-statement-cache-size> <share-prepared-statements>true</share-prepared-statements> </statement> </datasource> <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</connection-url> <driver>h2</driver> <security> <user-name>sa</user-name> <password>sa</password> </security> </datasource> <drivers> <driver name="com.mysql" module="com.mysql"> <driver-class>com.mysql.jdbc.Driver</driver-class> <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class> </driver> <driver name="h2" module="com.h2database.h2"> <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class> </driver> </drivers> </datasources> </subsystem> 

這是我的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="primary"> <jta-data-source>java:jboss/datasources/studadminDS</jta-data-source> <properties> <property name="hibernate.hbm2ddl.auto" value="update" /> <property name="hibernate.show_sql" value="true" /> </properties> </persistence-unit> </persistence> 

我只是使用此方法從數據庫中讀取:

@PostConstruct
public void init() {
    students = new ListDataModel<Student>();
    students.setWrappedData(em.createNamedQuery("SelectStudents")
            .getResultList());
}

而這個將學生持久存儲在數據庫中:

public String saveStudent() {
    try {
        utx.begin();
    } catch (NotSupportedException e) {
        e.printStackTrace();
    } catch (SystemException e) {
        e.printStackTrace();
    }
    student = em.merge(student);
    em.persist(student);
    students.setWrappedData(em.createNamedQuery("SelectStudents")
            .getResultList());
    try {
        utx.commit();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (RollbackException e) {
        e.printStackTrace();
    } catch (HeuristicMixedException e) {
        e.printStackTrace();
    } catch (HeuristicRollbackException e) {
        e.printStackTrace();
    } catch (SystemException e) {
        e.printStackTrace();
    }
    return "studentList";
}

在使用相應的方面將一些新學生保存在數據庫中之后,它們會成功列出。 但是問題是,當我在MySQL數據庫中查找時,沒有新條目,盡管數據保留在某處,因為如果部署我的應用程序,則數據仍然可用。

因此,很明顯我的應用程序正在使用哪個數據庫,或者可以檢測到它嗎?

您混合了JNDI名稱。

您的standalone.xml正在創建JNDI名稱java:/MySqlDS ,但是在persistence.xml中,您引用了JNDI資源java:jboss/datasources/studadminDS

我建議您更改standalone.xml:

...
<datasource jta="true" jndi-name="java:jboss/datasources/studadminDS" pool-name="MySqlDS_Pool" enabled="true" use-java-context="true" use-ccm="true">
...

在standalone.xml中聲明休眠方言也可能會有所幫助:

     <datasource ...>
           <connection-property name="hibernate.dialect">
                    org.hibernate.dialect.MySQLDialect
            </connection-property>
      </datasource ...>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM