抛出未知服务在DAO实现中的getCourses方法中打开会话时出现异常。当它从后端获取输入时工作正常但是当我从UI调用它时它会抛出此异常
的hibernate.cfg.xml `
<!-- hibernate dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@10.123.79.59:1521:georli04</property>
<property name="hibernate.connection.username">username</property>
<property name="hibernate.connection.password">password</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<!-- Automatic schema creation (begin) === -->
<property name="hibernate.hbm2ddl.auto">none</property>
<!-- Simple memory-only cache -->
<property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<mapping class="com.infy.entity.StudentEntity"/>
<mapping class="com.infy.entity.CourseEntity"/>
`
DAO实现当我们尝试在getCourses方法
中打开会话时发生异常public class StudentDAOImpl implements StudentDAO {
public Student getStudentDetails(String studentId) throws Exception {
SessionFactory sessionFactory = null;
Session session = null;
Student student = null;
try {
sessionFactory = HibernateUtility.createSessionFactory();
session = sessionFactory.openSession();
StudentEntity se = (StudentEntity) session.get(StudentEntity.class,
studentId);
if (se != null) {
student = new Student();
student.setStudentId(se.getStudentId());
student.setDateOfBirth(se.getDateOfBirth());
System.out.println(se.getDateOfBirth());
student.setStudentName(se.getStudentName());
System.out.println(se.getStudentName());
student.setBranch(se.getBranch());
student.setSem(se.getSem());
student.setEmail(se.getEmail());
}
} catch (HibernateException exception) {
exception.printStackTrace();
DOMConfigurator.configure("src/resources/log4j.xml");
Logger logger = Logger.getLogger(this.getClass());
logger.error(exception.getMessage(), exception);
throw new Exception("DAO.TECHNICAL_ERROR");
} catch (Exception exception) {
exception.printStackTrace();
DOMConfigurator.configure("src/resources/log4j.xml");
Logger logger = Logger.getLogger(this.getClass());
logger.error(exception.getMessage(), exception);
throw exception;
} finally {
if (session.isOpen() || session != null) {
session.close();
}
}
return student;
}
@SuppressWarnings("unchecked")
public List<Course> getCourses(int semester) throws Exception {
SessionFactory sessionFactory = HibernateUtility.createSessionFactory();
Session session = null;
List<CourseEntity> courseEntities = new LinkedList<CourseEntity>();
List<Course> courses = new LinkedList<Course>();
String hql = "from CourseEntity ce where ce.semester='" + semester
+ "'";
try {
System.out.println("before session");
session = sessionFactory.openSession();
System.out.println("after session");
Query q = session.createQuery(hql);
courseEntities = q.list();
if (courseEntities.isEmpty())
System.out.println("empty");
for (CourseEntity i : courseEntities) {
Course course = new Course();
course.setCourseId(i.getCourseId());
course.setCourseName(i.getCourseName());
course.setCourseType(i.getCourseType());
course.setSemester(i.getSemester());
course.setBranch(i.getBranch());
courses.add(course);
}
} catch (HibernateException exception) {
exception.printStackTrace();
DOMConfigurator.configure("src/resources/log4j.xml");
Logger logger = Logger.getLogger(this.getClass());
logger.error(exception.getMessage(), exception);
throw new Exception("DAO.TECHNICAL_ERROR");
} catch (Exception exception) {
exception.printStackTrace();
DOMConfigurator.configure("src/resources/log4j.xml");
Logger logger = Logger.getLogger(this.getClass());
logger.error(exception.getMessage(), exception);
throw exception;
}finally {
if (session.isOpen() || session != null)
session.close();
}
return courses;
}
例外
org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.engine.jdbc.connections.spi.ConnectionProvider]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:201)
at org.hibernate.internal.AbstractSessionImpl.getJdbcConnectionAccess(AbstractSessionImpl.java:341)
at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.<init>(JdbcCoordinatorImpl.java:114)
at org.hibernate.engine.transaction.internal.TransactionCoordinatorImpl.<init>(TransactionCoordinatorImpl.java:89)
at org.hibernate.internal.SessionImpl.<init>(SessionImpl.java:258)
at org.hibernate.internal.SessionFactoryImpl$SessionBuilderImpl.openSession(SessionFactoryImpl.java:1589)
at org.hibernate.internal.SessionFactoryImpl.openSession(SessionFactoryImpl.java:999)
at com.infy.dao.StudentDAOImpl.getCourses(StudentDAOImpl.java:74)
at com.infy.service.StudentServiceImpl.getListOfCourses(StudentServiceImpl.java:73)
at com.infy.api.RegistrationAPI.getCoursesList(RegistrationAPI.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161)
at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:160)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:317)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:298)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:471)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:425)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:383)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:336)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:223)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:957)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
答案 0 :(得分:0)
尝试使用数据库连接提供程序,例如c3p0,
添加到pom.xml
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.2.1</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>4.3.6.Final</version>
</dependency>
将这些行添加到hibernate.cfg.xml
中 <property name= "hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.acquire_increment">5</property>
<property name="hibernate.c3p0.idle_test_period">1800</property>
<property name="hibernate.c3p0.max_size">600</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.timeout">1800</property>