我在Weblogic Server 12c上创建了一个JNDI。我正在尝试使用数据源连接到服务器上的Oracle数据库,并使用休眠模式在其中创建表。但是我仍然遇到相同的异常 org.hibernate.service.spi.ServiceException:无法创建请求的服务[org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] Part 1 {{ 3}}。我从index.jsp提交ID和名称后出现此异常 请查看我的Java代码并提出任何建议:
版本: Java 8 Weblogic 12c 休眠5 甲骨文11g
Hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.datasource">java:comp/env/jdbc/Abc</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<mapping class="packone.Bean"/>
</session-factory>
</hibernate-configuration>
Context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/Abc"
auth="container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@//RAPC01-1702-26:1521/P1130PL2"
username="root"
password="root"
maxActive="100"
maxIdle="20"
minIdle="5"
maxWait="10000">
</Resource>
</Context>
HibernateUtil.java:
private static StandardServiceRegistry standardServiceRegistry;
private static SessionFactory sessionFactory;
public Session getSession(List<String> list) {
list.add("START: getsession");
if(sessionFactory == null) {
try {
standardServiceRegistry = new StandardServiceRegistryBuilder().configure().build();
MetadataSources metadataSources = new MetadataSources(standardServiceRegistry);
Metadata metadata = metadataSources.getMetadataBuilder().build();
sessionFactory = metadata.getSessionFactoryBuilder().build();
} catch(Exception e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
list.add(sw.toString());
}
}
list.add("END: getsession");
return sessionFactory.openSession();
}
web.xml: 只是包含欢迎文件列表为index.jsp
index.jsp:
<%
List<String> list = (List<String>)request.getAttribute("lsit");
if(list==null) {
} else {
for(String str: list) {
%> <%=str %><br> <%
}
}
%>
<form action="BeanServlet">
<input type="text" name="id" placeholder="Enter Id...">
<input type="text" name="name" placeholder="Enter Name...">
<input type="submit">
</form>
Bean.java:
package packone;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Bean {
@Id
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
BeanServlet.java:
List<String> list = new ArrayList<>();
list.add("servlet started");
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
list.add("id value: "+id);
list.add("name value: "+name);
Bean B = new Bean();
B.setId(id);
B.setName(name);
new BeanDAO().createBean(B, list);
list.add("servlet before RD");
request.setAttribute("lsit", list);
RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
rd.forward(request, response);
BeanDAO.java:
package packone;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;
import org.hibernate.Session;
public class BeanDAO {
public void createBean(Bean B, List<String> list) {
Session session = null;
try {
list.add("START: DAO");
session = new HibernateUtil().getSession(list);
list.add("Arriva Session");
session.beginTransaction();
session.save(B);
session.getTransaction().commit();
list.add("END: DAO");
} catch(Exception e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
list.add(sw.toString());
} finally {
if(session != null) {
session.flush();
session.close();
}
}
}
}
让我知道您是否需要更多详细信息或问题格式是否需要改进。不是Weblogic btw的专家。谢谢。
答案 0 :(得分:0)
对我来说一切都很好。
尝试使用ojdbc6.jar而不是ojdbc14.jar。因为您的Oracle版本是11,而Java是8。ojdbc14.jar现在非常老。
让我知道它是否有效。