我尝试从1.7.4的Apache TomEE Plume迁移到8.0.0-M2,有些事情在以前的版本中起作用,而在新版本中不起作用。
我在context.xml
中有资源
<Resource
auth="Container"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
maxTotal="10000"
maxIdle="1000"
maxPoolSize="10000"
maxWaitMillis="-1"
name="jdbc/datasource"
password="password"
type="javax.sql.DataSource"
url="jdbc:sqlserver://127.0.0.1:1433;databaseName=database;applicationName=app"
username="usr"
validationQuery="select @@version"/>
它用作persistence.xml文件中的数据源
<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="gispte" transaction-type="RESOURCE_LOCAL">
<non-jta-data-source>jdbc/datasource</non-jta-data-source>
<class>model.UserContactType</class>
</persistence-unit>
</persistence>
这种配置在1.7.4中可以很好地工作,并且在8.0.0中进行数据访问时会引发错误
EntityManager em = factory.createEntityManager();
TypedQuery<UserContactType> q = em.createQuery("select uct from UserContactType uct order by uct.name", UserContactType.class);
return q.getResultList();
控制台输出
[EL Warning]: 2019-05-01 18:35:52.725--UnitOfWork(1787752065)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.4.v20160829-44060b6): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: USERCONTACTTYPE
Error Code: -5501
Call: SELECT id, name FROM usercontacttype ORDER BY name
Query: ReadAllQuery(referenceClass=UserContactType sql="SELECT id, name FROM usercontacttype ORDER BY name")
мая 01, 2019 6:35:52 PM org.apache.openejb.core.transaction.EjbTransactionUtil handleSystemException
SEVERE: EjbTransactionUtil.handleSystemException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.4.v20160829-44060b6): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: USERCONTACTTYPE
Error Code: -5501
Call: SELECT id, name FROM usercontacttype ORDER BY name
Query: ReadAllQuery(referenceClass=UserContactType sql="SELECT id, name FROM usercontacttype ORDER BY name")
javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.4.v20160829-44060b6): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: USERCONTACTTYPE
Error Code: -5501
Call: SELECT id, name FROM usercontacttype ORDER BY name
Query: ReadAllQuery(referenceClass=UserContactType sql="SELECT id, name FROM usercontacttype ORDER BY name")
at org.eclipse.persistence.internal.jpa.QueryImpl.getDetailedException(QueryImpl.java:382)
at org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(QueryImpl.java:260)
.....
Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.4.v20160829-44060b6): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: USERCONTACTTYPE
Error Code: -5501
Call: SELECT id, name FROM usercontacttype ORDER BY name
Query: ReadAllQuery(referenceClass=UserContactType sql="SELECT id, name FROM usercontacttype ORDER BY name")
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:340)
.....
Caused by: java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: USERCONTACTTYPE
at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
.....
在此日志中,可以清楚地看到JPA尝试连接到HSQL数据库,尽管在配置中指向MS SQL。我试图在实体管理器中查看连接字符串及其真实性。
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
try {
Connection con = em.unwrap(java.sql.Connection.class);
if ( con == null ) {
System.out.println("failed get connection from entity manager");
} else {
System.out.println("connection url from entity manager\n"+con.getMetaData().getURL());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
em.getTransaction().commit();
}
控制台输出8.0.0
connection url from entity manager
jdbc:hsqldb:file:D:\src\.metadata\.plugins\org.eclipse.wst.server.core\tmp2\data\hsqldb\hsqldb
但是在tomee 1.7.4中工作是正确的
connection url from entity manager
jdbc:sqlserver://127.0.0.1:1433;authenticationScheme=nativeAuthentication;xopenStates=false;sendTimeAsDatetime=true;trustServerCertificate=false;sendStringParametersAsUnicode=true;selectMethod=direct;responseBuffering=adaptive;packetSize=8000;multiSubnetFailover=false;loginTimeout=15;lockTimeout=-1;lastUpdateCount=true;encrypt=false;disableStatementPooling=true;databaseName=database;applicationName=app;applicationIntent=readwrite;
8.0.0中的哪些新设置需要更改才能正常工作?为什么正确的连接更改为hsql连接?也许这是此运行时中的默认连接,并且tomee 8.0.0只是读取旧设置失败,不是吗?
服务器启动时也出现警告
may 01, 2019 7:38:58 PM sun.reflect.NativeMethodAccessorImpl invoke
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:app' did not find a matching property.
.....
may 01, 2019 7:32:20 PM org.apache.openejb.config.AutoConfig deploy
WARNING: Found matching datasource: app/jdbc/datasource but this one is a JTA datasource
may 01, 2019 7:32:20 PM org.apache.openejb.config.AutoConfig deploy
WARNING: Found matching datasource: ROOT/jdbc/datasource but this one is a JTA datasource
may 01, 2019 7:32:20 PM org.apache.openejb.config.AutoConfig deploy
WARNING: Found matching datasource: ROOT/jdbc/datasource but this one is a JTA datasource