使用Jetty和JNDI / JPA连接到数据库

时间:2015-07-23 17:20:25

标签: java jpa jetty

我正试图从玻璃鱼迁移到码头。我试图在jetty中设置连接池以使用JPA访问oracle数据库。当我在persistence.xml中拥有所有连接信息时,连接正常,我可以访问数据库,因此我知道数据库驱动程序正在运行。但是,有些应用程序使用JNDI查找数据源,如下例所示。

当我运行它时,EntityManager在em.createNativeQuery(queryString,Config.class).getResultList();中为null。

的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
<persistence-unit name="DATA_SOURCE" transaction-type="JTA">
    <jta-data-source>testData</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
        <property name="eclipselink.cache.shared.default" value="false"/>
    </properties>
</persistence-unit>

爪哇:

@ManagedBean(name = "configDAO")
@ApplicationScoped
public class ConfigDAO implements Serializable {

    @PersistenceContext()
    private EntityManager em;
    @Resource
    private UserTransaction utx;

    public String getGroupEmailList() {
        return getConfigByName(FileUtils.EMAIL_LIST).getConfigValue();
    }

    public Config getConfigByName(String configName) {
        String queryString = "SELECT * FROM CONFIG WHERE CONFIG_NAME = '" + configName + "'";
        List<Config> configList = em.createNativeQuery(queryString, Config.class).getResultList(); 
        if (configList != null && !configList.isEmpty()) {
            return configList.get(0);
        }

        return null;
    }

}

在$ JETTY_HOME / etc

中的jetty.xml中
<New id="testData" class="org.eclipse.jetty.plus.jndi.Resource">
    <Arg/>
    <Arg>testData</Arg>
    <Arg>
        <New class="oracle.jdbc.pool.OracleDataSource">
            <Set name="URL">jdbc:oracle:thin:@server:1521:data</Set>
            <Set name="User">user</Set>
            <Set name="Password">password</Set>
            <Set name="ConnectionCachingEnabled">true</Set>
            <Set name="ConnectionCacheProperties">
                <New class="java.util.Properties">
                    <Call name="setProperty">
                        <Arg type="java.lang.String">InactivityTimeout</Arg>
                        <Arg type="java.lang.String">300</Arg>
                    </Call>
                    <Call name="setProperty">
                        <Arg type="java.lang.String">AbandonedConnectionTimeout</Arg>
                        <Arg type="java.lang.String">600</Arg>
                    </Call>
                    <Call name="setProperty">
                        <Arg type="java.lang.String">TimeToLiveTimeout</Arg>
                        <Arg type="java.lang.String">600</Arg>
                    </Call>
                    <Call name="setProperty">
                        <Arg type="java.lang.String">InitialLimit</Arg>
                        <Arg type="java.lang.String">10</Arg>
                    </Call>
                    <Call name="setProperty">
                        <Arg type="java.lang.String">MaxStatementsLimit</Arg>
                        <Arg type="java.lang.String">50</Arg>
                    </Call>
                    <Call name="setProperty">
                        <Arg type="java.lang.String">MinLimit</Arg>
                        <Arg type="java.lang.String">20</Arg>
                    </Call>
                </New>
            </Set>
        </New>
    </Arg>
</New>

0 个答案:

没有答案