如题,调用远程接口报出 Exception in thread "main" javax.ejb.EJBException: javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: Cannot open connection
at org.jboss.ejb3.tx.Ejb3TxPolicy.handleExceptionInOurTx(Ejb3TxPolicy.java:63)
at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:83)
at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:191)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:95)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:62)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:77)
at org.jboss.ejb3.security.Ejb3AuthenticationInterceptor.invoke(Ejb3AuthenticationInterceptor.java:110)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:46)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessContainer.java:304)
at org.jboss.aop.Dispatcher.invoke(Dispatcher.java:106)
at org.jboss.aspects.remoting.AOPRemotingInvocationHandler.invoke(AOPRemotingInvocationHandler.java:82)
at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:809)
at org.jboss.remoting.transport.socket.ServerThread.processInvocation(ServerThread.java:608)
at org.jboss.remoting.transport.socket.ServerThread.dorun(ServerThread.java:406)
at org.jboss.remoting.transport.socket.ServerThread.run(ServerThread.java:173)
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: Cannot open connection 我的mysql-ds.xml 为
<datasources>
<local-tx-datasource>
<jndi-name>MySqlDS </jndi-name>
<connection-url>jdbc:mysql://localhost:3306/ejb3 </connection-url>
<driver-class>com.mysql.jdbc.Driver </driver-class>
<user-name>root </user-name>
<password>test </password>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter </exception-sorter-class-name>
<metadata>
<type-mapping>mySQL </type-mapping>
</metadata>
</local-tx-datasource>
<local-tx-datasource>
<jndi-name>MySqlDS2 </jndi-name>
<connection-url>jdbc:mysql://localhost:3306/ejb32 </connection-url>
<driver-class>com.mysql.jdbc.Driver </driver-class>
<user-name>root </user-name>
<password>test </password>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter </exception-sorter-class-name>
<metadata>
<type-mapping>mySQL </type-mapping>
</metadata>
</local-tx-datasource>
</datasources> 我的persistence.xml 配置: <persistence-unit name="test" transaction-type="JTA">
<jta-data-source>java:/MySqlDS </jta-data-source>
<class>com.ni.jpa.User </class>
<exclude-unlisted-classes>true </exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
<persistence-unit name="test2" transaction-type="JTA">
<jta-data-source>java:/MySqlDS2 </jta-data-source>
<class>com.ni.jpa.Person </class>
<exclude-unlisted-classes>true </exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit> 调用的UserManagerBean 的addUser方法,我注入了针对2个数据源的EntityManager
@Remote(UserManager.class)
@Stateless(name="UserManager")
public class UserManagerBean implements UserManager {
// 注入EntityManager
@PersistenceContext(unitName = "test")
private EntityManager em; @PersistenceContext(unitName = "test2")
private EntityManager em2; public void addUser(String name) {
User user = new User();
user.setName(name+"_User");
em.persist(user); Person p = new Person();
p.setName(name+"_Person");
em2.persist(p);
}
我想调用addUser方法把2个对象放在指定的数据库中,一运行就会发现这个错误,在网上也找过,没发现相类似的,同一个事务中对同一个数据库的不同的数据源插入数据报出Cannot open connection,我在事务中只放了一个插入(user或者person)对象的方法,都能成功,就是2个放在一个事务中不行啊,高手求救啊
at org.jboss.ejb3.tx.Ejb3TxPolicy.handleExceptionInOurTx(Ejb3TxPolicy.java:63)
at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:83)
at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:191)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:95)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:62)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:77)
at org.jboss.ejb3.security.Ejb3AuthenticationInterceptor.invoke(Ejb3AuthenticationInterceptor.java:110)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:46)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessContainer.java:304)
at org.jboss.aop.Dispatcher.invoke(Dispatcher.java:106)
at org.jboss.aspects.remoting.AOPRemotingInvocationHandler.invoke(AOPRemotingInvocationHandler.java:82)
at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:809)
at org.jboss.remoting.transport.socket.ServerThread.processInvocation(ServerThread.java:608)
at org.jboss.remoting.transport.socket.ServerThread.dorun(ServerThread.java:406)
at org.jboss.remoting.transport.socket.ServerThread.run(ServerThread.java:173)
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: Cannot open connection 我的mysql-ds.xml 为
<datasources>
<local-tx-datasource>
<jndi-name>MySqlDS </jndi-name>
<connection-url>jdbc:mysql://localhost:3306/ejb3 </connection-url>
<driver-class>com.mysql.jdbc.Driver </driver-class>
<user-name>root </user-name>
<password>test </password>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter </exception-sorter-class-name>
<metadata>
<type-mapping>mySQL </type-mapping>
</metadata>
</local-tx-datasource>
<local-tx-datasource>
<jndi-name>MySqlDS2 </jndi-name>
<connection-url>jdbc:mysql://localhost:3306/ejb32 </connection-url>
<driver-class>com.mysql.jdbc.Driver </driver-class>
<user-name>root </user-name>
<password>test </password>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter </exception-sorter-class-name>
<metadata>
<type-mapping>mySQL </type-mapping>
</metadata>
</local-tx-datasource>
</datasources> 我的persistence.xml 配置: <persistence-unit name="test" transaction-type="JTA">
<jta-data-source>java:/MySqlDS </jta-data-source>
<class>com.ni.jpa.User </class>
<exclude-unlisted-classes>true </exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
<persistence-unit name="test2" transaction-type="JTA">
<jta-data-source>java:/MySqlDS2 </jta-data-source>
<class>com.ni.jpa.Person </class>
<exclude-unlisted-classes>true </exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit> 调用的UserManagerBean 的addUser方法,我注入了针对2个数据源的EntityManager
@Remote(UserManager.class)
@Stateless(name="UserManager")
public class UserManagerBean implements UserManager {
// 注入EntityManager
@PersistenceContext(unitName = "test")
private EntityManager em; @PersistenceContext(unitName = "test2")
private EntityManager em2; public void addUser(String name) {
User user = new User();
user.setName(name+"_User");
em.persist(user); Person p = new Person();
p.setName(name+"_Person");
em2.persist(p);
}
我想调用addUser方法把2个对象放在指定的数据库中,一运行就会发现这个错误,在网上也找过,没发现相类似的,同一个事务中对同一个数据库的不同的数据源插入数据报出Cannot open connection,我在事务中只放了一个插入(user或者person)对象的方法,都能成功,就是2个放在一个事务中不行啊,高手求救啊
解决方案 »
- java 递归查询子节点
- hibernate 自定义数据类型 在实现序列化接口后还是抛出不能反序列化的异常
- 求助:Orcale表设置两个clob,两个clob字段的内容颠倒了!!!!!
- 关于在java中写的模糊查询语句
- 用java如何将数据库中的数据快速导出到excel
- 又惊天一问!怎么解析JSF中的表达式。myfaces中,我做了个组件,具体如下:
- 怎么开发英文版网站
- 在开发项目中,不管是开发还是系统运行,log是必不可少的。怎样利用log4g做一个便于程序员调用的,内容丰富的log
- Could not find a getter for extEmployee_no in class com.wangzhe.pojo.……
- struts1.1下多个模块配置struts-config.xml,不认jsp,how to?
- 反射可以得到什么,清明细
- extjs tree DWRTreeLoader从数据库读出来的叶节点在页面怎么不能添加子节点啊,但读出来的节点就可以添加子节点
<datasources>
<xa-datasource>
<jndi-name>MySqlDS </jndi-name>
<xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource </xa-datasource-class>
<xa-datasource-property name="Url">jdbc:mysql://localhost:3306/ejb </xa-datasource-property>
<xa-datasource-property name="User">root </xa-datasource-property>
<xa-datasource-property name="Password">root </xa-datasource-property>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter </exception-sorter-class-name>
<metadata>
<type-mapping>mySQL </type-mapping>
</metadata>
</xa-datasource> <xa-datasource>
<jndi-name>MySqlDS2 </jndi-name>
<xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource </xa-datasource-class>
<xa-datasource-property name="Url">jdbc:mysql://localhost:3306/ejb2 </xa-datasource-property>
<xa-datasource-property name="User">root </xa-datasource-property>
<xa-datasource-property name="Password">root </xa-datasource-property>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter </exception-sorter-class-name>
<metadata>
<type-mapping>mySQL </type-mapping>
</metadata>
</xa-datasource>
</datasources> 重启JBoss,搞定。