gpt4 book ai didi

java - 我正在使用 hibernate 4.3.8 和 swing 应用程序并获取 org.hibernate.service.UnknownServiceException

转载 作者:太空宇宙 更新时间:2023-11-04 14:13:28 25 4
gpt4 key购买 nike

请建议我一种解决此问题的方法。

我在使用单线程的控制台应用程序中没有收到此错误,但当我在 swing 多线程应用程序中使用相同的代码时,我收到了此异常。

我在这段代码中遇到错误

Session session = HibernateUtilities.getSessionFactory().openSession();

这是 HibernateUtilities 类

public class HibernateUtilities {

private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;

private void createSessionFactory() {
try {
String configXmlPath = "hibernate/hibernate.cfg.xml";

Configuration configuration = new Configuration()
.configure(configXmlPath);

serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties())
.build();

sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}

public static SessionFactory getSessionFactory() {
if (sessionFactory == null) {
new HibernateUtilities().createSessionFactory();
}
return sessionFactory;
}

}

这是配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306</property>
<property name="hibernate.default_schema">school</property>
<property name="hibernate.connection.username">demo</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="hibernate.current_session_context_class">thread</property>


<mapping class="model.Student"/>
<mapping class="model.Teacher"/>
<mapping class="model.Course"/>

</session-factory>
</hibernate-configuration>

类(class)形式:

package view;

import hibernate.HibernateUtilities;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import model.Course;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import utilities.ButtonColumn;
import utilities.CourseTableModel;
import utilities.EmptyTextFieldException;

public class CourseForm extends javax.swing.JFrame {

private List<Course> courses;

public CourseForm() {
setCourses();
initComponents();
addActionListeners();
}

private void setCourses() {
try {
Session session = HibernateUtilities.getSessionFactory().openSession();
Criteria criteria = session.createCriteria(Course.class);
courses = criteria.list();

} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(rootPane, ex.getMessage());
}
}

private void addActionListeners() {
ButtonColumn buttonColumn = new ButtonColumn(coursesTable, null, 2);
coursesTable.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent me) {
JTable table = (JTable) me.getSource();
Point p = me.getPoint();
int rowIndex = table.rowAtPoint(p);
if (me.getClickCount() > 0) {
if (table.columnAtPoint(p) == 2) {
deleteCourseInformation(rowIndex);
} else {
updateCourseInformation(rowIndex);
}
}
}
});
}

private void deleteCourseInformation(int rowIndex) {
String courseId = (String) coursesTable.getModel().getValueAt(rowIndex, 0);
Session session = HibernateUtilities.getSessionFactory().openSession();
session.beginTransaction();
Criteria criteria = session.createCriteria(Course.class)
.add(Restrictions.eq("id", courseId));
Course courseToDelete = (Course) criteria.uniqueResult();
session.delete(courseToDelete);
session.getTransaction().commit();

((CourseTableModel) coursesTable.getModel()).removeRow(rowIndex);
}

private void updateCourseInformation(int rowIndex) {
String courseTitle = (String) coursesTable.getValueAt(rowIndex, 1);
courseTitleField.setText(courseTitle);

String courseId = (String) coursesTable.getValueAt(rowIndex, 0);
courseIdField.setText(courseId);
courseIdField.setEditable(false);

addButton.setText("Update");
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jLabel3 = new javax.swing.JLabel();
courseTitleField = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
coursesTable = new javax.swing.JTable();
addButton = new javax.swing.JButton();
jLabel5 = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
courseIdField = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
resetButton = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

jLabel3.setText("Course Title:");

courseTitleField.setName("Course Title"); // NOI18N

coursesTable.setModel(new CourseTableModel(courses, new String[] {"ID","Title", "Delete"}));
jScrollPane1.setViewportView(coursesTable);

addButton.setText("Add");
addButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addButtonActionPerformed(evt);
}
});

jLabel5.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jLabel5.setText("Course Information:");

jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jLabel1.setText("All Courses:");

courseIdField.setName("Course ID"); // NOI18N

jLabel4.setText("Course ID:");

resetButton.setText("Reset");
resetButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
resetButtonActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1)
.addComponent(jLabel5)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(courseIdField, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(jLabel3)
.addGap(32, 32, 32)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(resetButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(addButton))
.addComponent(courseTitleField, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(21, 21, 21)
.addComponent(jLabel5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(courseIdField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(courseTitleField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(16, 16, 16)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(addButton)
.addComponent(resetButton))
.addGap(33, 33, 33)
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 332, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private String getTextFieldValue(JTextField textField) throws EmptyTextFieldException {
String value = textField.getText().trim();

if (value == null || value.isEmpty()) {
throw new EmptyTextFieldException(textField.getName());
}

return value;
}

private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {
try {
String courseId = getTextFieldValue(courseIdField);
String courseTitle = getTextFieldValue(courseTitleField);

Course course = new Course();
course.setId(courseId);
course.setName(courseTitle);

if (addButton.getText().equalsIgnoreCase("Add")) {
Session session = HibernateUtilities.getSessionFactory().openSession();
session.beginTransaction();
session.saveOrUpdate(course);
session.getTransaction().commit();

((CourseTableModel) coursesTable.getModel()).addRow(course);
} else if (addButton.getText().equalsIgnoreCase("Update")) {
Session session = HibernateUtilities.getSessionFactory().openSession();
session.beginTransaction();
session.saveOrUpdate(course);
Criteria criteria = session.createCriteria(Course.class);
courses = criteria.list();
session.getTransaction().commit();

((CourseTableModel) coursesTable.getModel()).setCourses(courses);
}
} catch (EmptyTextFieldException ex) {
JOptionPane.showMessageDialog(rootPane, ex.getMessage());
}
}

private void resetButtonActionPerformed(java.awt.event.ActionEvent evt) {
courseIdField.setText("");
courseIdField.setEditable(true);
courseTitleField.setText("");
addButton.setText("Add");
}

public static void display() {
new CourseForm().setVisible(true);
}

// Variables declaration - do not modify
private javax.swing.JButton addButton;
private javax.swing.JTextField courseIdField;
private javax.swing.JTextField courseTitleField;
private javax.swing.JTable coursesTable;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JButton resetButton;
// End of variables declaration
}

回溯:

Exception in thread "AWT-EventQueue-0" 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 controller.Repository.save(Repository.java:45)
at view.CourseForm.addButtonActionPerformed(CourseForm.java:202)
at view.CourseForm.access$200(CourseForm.java:16)
at view.CourseForm$2.actionPerformed(CourseForm.java:108)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.awt.EventQueue$3.run(EventQueue.java:686)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:702)
at java.awt.EventQueue$4.run(EventQueue.java:700)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

最佳答案

我怀疑多线程中存在竞争条件,因为您说它在单线程控制台应用程序中运行良好。

public static SessionFactory getSessionFactory() {
synchronized(HibernateUtilities.class) {
if (sessionFactory == null) {
new HibernateUtilities().createSessionFactory();
}
}
return sessionFactory;
}

您可能需要同步对 sessionFactory 的访问,因为多个线程可以同时调用它并创建多个实例。

关于java - 我正在使用 hibernate 4.3.8 和 swing 应用程序并获取 org.hibernate.service.UnknownServiceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28015827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com