我们正在使用JPA 2.0,并在Websphere中创建了数据源,并尝试通过J2SE应用程序访问数据库。我们收到无效的用户名和密码错误。如果我们在persistence.xml中输入用户名和密码,则可以正常工作。

请有人解释我,因为我们有数据源,为什么我们必须在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="Printer">

            <jta-data-source>jdbc/TestDataSource</jta-data-source>

            <properties>
                  <property name="openjpa.Optimistic" value="false" />
                  <property name="openjpa.LockManager" value="pessimistic" />
                  <property name="javax.persistence.jdbc.user" value="admin" />
                  <property name="javax.persistence.jdbc.password" value="admin#2" />
                </properties>

      </persistence-unit>
</persistence>

最佳答案

似乎在Websphere中没有正确配置数据源。通过Websphere控制台Test the database connection验证配置。

DataSource有两种获取Connection的方法。如果您通过属性提供凭据,则您的jpa实现似乎使用了DataSource.getConnection(String username, String password)

旨在在Java SE环境中使用连接属性。在JEE中,您应该首选JNDI查找。请参阅JPA 2.0规范的8.2.1.9部分。

07-24 18:47
查看更多