我正在尝试让 JavaEE 项目在 Glassfish 4.0 上运行,但是当我尝试将插入(持久)刷新到 Sql Server DB 时,它会引发此错误:
org.omg.CORBA.MARSHAL: WARNING: 00810057: Could not load class com.microsoft.sqlserver.jdbc.SQLServerException vmcid: OMG minor code: 57 completed: Maybe
该连接在 Netbeans 中用于运行命令和生成实体类。
bean 法:
public int addUser(UserDetails u) {
try{
User uP = new User(u.getUserName(), u.isEnabled(), u.isAdmin());
em.persist(uP);
return 0;
}
catch(Exception e)
{
throw new EJBException(e);
}
}
持久性.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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="LibraryBeanPU" transaction-type="JTA">
<jta-data-source>JavaEE_Library</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
</persistence>
客户:
public static void main(String[] args) {
LibraryClient lc = new LibraryClient();
}
public LibraryClient()
{
LibraryFacadeRemote request = (LibraryFacadeRemote) getEJBBean("LibraryFacade");
request.addUser(new UserDetails("admin", true, true));
}
public Object getEJBBean(String beanName) {
try {
InitialContext ctx = new InitialContext();
return ctx.lookup(beanName);
} catch (Exception ex) {
System.err.println("ERROR: while locating bean from the server " + ex.getMessage());
return null;
}
}
最佳答案
persist
方法可能在服务器端失败,出现 com.microsoft.sqlserver.jdbc.SQLServerException
被捕获并包裹成 EJBException
.
当客户端代码收到异常时,它无法解码/反序列化 com.microsoft.sqlserver.jdbc.SQLServerException
实例,因为它不存在于客户端 ClassPath
.
在客户端中添加 MS SqlServer JDBC 驱动程序 ClassPath
应该可以解决这个问题,但我建议您避免将完整的服务器端异常传输到您的客户端代码,这要归功于以下架构指南:
EJBException
, 不包装原始异常而是创建错误消息 String
来自原文 getMessage()
和 UUID 指的是记录的完整堆栈跟踪 关于jakarta-ee - 无法加载类 - JDBC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20725093/