我正在使用Wildfly 9.X玩JTA交易。
我可以通过
获取UserTransactionut = (UserTransaction)new InitialContext().lookup("java:comp/UserTransaction");
然后我想用我的实体经理在我的dao类中使用它: ut.begin(); em.joinTransaction(); em.persist(NEWUSER); ut.commit();
但我希望得到这个例外:
Caused by: org.h2.jdbc.JdbcSQLException: Table "MAP_USER" not found; SQL statement:
insert into MAP_USER (ADS_USER, BAD_PASSWORD_COUNT, COST_CENTRE, DB_PASSWORD, DB_USER, department, email, ENABLED_YN, EXPIRING_DATE, EXTERNAL_ID, EXTERNAL_LAST_SYNC, EXTERNAL_REPOSITORY , fax, FULL_NAME, LAST_BAD_PASSWORD, LAST_LOGIN, LAST_PASSWORD_CHANGE, password, PASSWORD_HISTORY, PROFILE_CODE, PROFILE_CODE_COMMENT, PROFILE_USER_YN, tel, USER_COMMENT, username) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [42102-173]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:331)
at org.h2.message.DbException.get(DbException.java:171)
at org.h2.message.DbException.get(DbException.java:148)
at org.h2.command.Parser.readTableOrView(Parser.java:4864)
at org.h2.command.Parser.readTableOrView(Parser.java:4842)
at org.h2.command.Parser.parseInsert(Parser.java:974)
at org.h2.command.Parser.parsePrepared(Parser.java:375)
at org.h2.command.Parser.parse(Parser.java:279)
at org.h2.command.Parser.parse(Parser.java:251)
at org.h2.command.Parser.prepareCommand(Parser.java:218)
at org.h2.engine.Session.prepareLocal(Session.java:428)
at org.h2.engine.Session.prepareCommand(Session.java:377)
at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1138)
at org.h2.jdbc.JdbcPreparedStatement.<init>(JdbcPreparedStatement.java:70)
at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:644)
at org.jboss.jca.adapters.jdbc.BaseWrapperManagedConnection.doPrepareStatement(BaseWrapperManagedConnection.java:800)
at org.jboss.jca.adapters.jdbc.BaseWrapperManagedConnection.prepareStatement(BaseWrapperManagedConnection.java:786)
at org.jboss.jca.adapters.jdbc.WrappedConnection.prepareStatement(WrappedConnection.java:434)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$1.doPrepare(StatementPreparerImpl.java:103)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:186)
... 87 more
这是我的数据源:
<xa-datasource jndi-name="java:jboss/datasources/KasOracleDS" pool-name="KasOracleDS" enabled="true" use-ccm="true">
<xa-datasource-property name="URL">
jdbc:oracle:thin:@localhost:1521:xe
</xa-datasource-property>
<xa-datasource-class>oracle.jdbc.xa.client.OracleXADataSource</xa-datasource-class>
<driver>oracle</driver>
<xa-pool>
<is-same-rm-override>false</is-same-rm-override>
<no-tx-separate-pools>true</no-tx-separate-pools>
</xa-pool>
<security>
<user-name>user</user-name>
<password>pwd</password>
</security>
<validation>
<valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleValidConnectionChecker"/>
<background-validation>true</background-validation>
<stale-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleStaleConnectionChecker"/>
<exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleExceptionSorter"/>
</validation>
</xa-datasource>
<drivers>
<driver name="oracle" module="com.oracle.ojdbc6">
<xa-datasource-class>oracle.jdbc.xa.client.OracleXADataSource</xa-datasource-class>
</driver>
<driver name="h2" module="com.h2database.h2">
<xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
</driver>
</drivers>
我已成功将ojdbc6驱动程序安装为wildfly模块。
为什么误差会因h2而上升?它试图连接的数据源是什么?
EDITED
我已通过以下代码行声明了我的实体经理:
Context ctx = new InitialContext();
EntityManagerFactory emfByJoss = (EntityManagerFactory)ctx.lookup(dao.getPersistenceJNDIUnitName());
EntityManager em = emfByJoss.createEntityManager(getJPAPropertiesForJboss(principal.getDataSource()));
private Properties getJPAPropertiesForJboss(String dataSourcePath) {
Properties properties = new Properties();
properties.put ("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
properties.put("javax.persistence.provider", "org.hibernate.jpa.HibernatePersistenceProvider");
properties.put("javax.persistence.transactionType", "JTA");
properties.put("javax.persistence.jtaDataSource", dataSourcePath);
return properties;
}
数据源路径是我在wildfly中创建的数据源的jndi名称,而持久性xml是:
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="KAS-Mapping-AppHandled">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.my.MapUser</class>
<properties>
<!-- Bind entity manager factory to JNDI at java:jboss/myEntityManagerFactory -->
<property name="hibernate.show_sql" value="true" />
<property name="jboss.entity.manager.factory.jndi.name" value="java:jboss/EntityManagerFactory" />
</properties>
</persistence-unit>
</persistence>
我被迫这样做,因为数据源必须是动态的并且在运行时更改。