我正在使用GWT RPC和Hibernate使用Eclipse环境从MySQL插入和检索数据。我已经在服务接口中编写了两种方法来从单个MySQL表插入和检索数据。
该程序运行正常,但引发了此异常。
Exception in thread "UnitCacheLoader" java.lang.RuntimeException: Unable to read from byte cache at com.google.gwt.dev.util.DiskCache.transferFromStream(DiskCache.java:166) at com.google.gwt.dev.util.DiskCacheToken.readObject(DiskCacheToken.java:87) at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at java.io.ObjectStreamClass.invokeReadObject(Unknown Source) at java.io.ObjectInputStream.readSerialData(Unknown Source) at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source) at java.io.ObjectInputStream.readObject0(Unknown Source) at java.io.ObjectInputStream.defaultReadFields(Unknown Source) at java.io.ObjectInputStream.readSerialData(Unknown Source) at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source) at java.io.ObjectInputStream.readObject0(Unknown Source) at java.io.ObjectInputStream.readObject(Unknown Source) at com.google.gwt.dev.javac.PersistentUnitCache.loadUnitMap(PersistentUnitCache.java:493) at com.google.gwt.dev.javac.PersistentUnitCache.access$000(PersistentUnitCache.java:92) at com.google.gwt.dev.javac.PersistentUnitCache$UnitCacheMapLoader.run(PersistentUnitCache.java:122) Caused by: java.io.StreamCorruptedException: unexpected EOF in middle of data block at java.io.ObjectInputStream$BlockDataInputStream.refill(Unknown Source) at java.io.ObjectInputStream$BlockDataInputStream.read(Unknown Source) at java.io.ObjectInputStream.read(Unknown Source) at java.io.InputStream.read(Unknown Source) at com.google.gwt.dev.util.DiskCache.transferFromStream(DiskCache.java:154) ... 16 more
ServiceImpl类:
package rpctest.server; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import com.google.gwt.user.server.rpc.RemoteServiceServlet; //import com.hib.HibernateUtil; import org.hibernate.Session; import org.hibernate.Transaction; import rpctest.shared.User; import rpctest.client.RpctestService; public class RpctestServiceImpl extends RemoteServiceServlet implements RpctestService { public String addUser(String name1, String name2) throws IllegalArgumentException { Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); User user = new User(); user.setFirstName(name1); user.setLastName(name2); session.save(user); session.getTransaction().commit(); } catch (RuntimeException e) { if(trns != null){ trns.rollback(); } e.printStackTrace(); } finally{ session.flush(); session.close(); } return name1+name2; // to test flextable entris only } @Override public User[] getUser() { List<User> getUser = null; Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); getUser = session.createQuery("from User").list(); //* for (Iterator<User> iter = getUser.iterator(); iter.hasNext();) //{ //User user = iter.next(); // //*} trns.commit(); } catch (RuntimeException e) { if(trns != null){ trns.rollback(); } e.printStackTrace(); } finally{ session.flush(); session.close(); } return getUser.toArray(new User[getUser.size()]); } }
入口点类:
package rpctest.client; import java.util.ArrayList; import rpctest.shared.User; import rpctest.shared.FieldVerifier; import com.google.gwt.core.client.EntryPoint; import com.google.gwt.core.client.GWT; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; import com.google.gwt.event.dom.client.KeyCodes; import com.google.gwt.event.dom.client.KeyPressEvent; import com.google.gwt.event.dom.client.KeyUpEvent; import com.google.gwt.event.dom.client.KeyUpHandler; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.rpc.AsyncCallback; import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.DialogBox; import com.google.gwt.user.client.ui.FlexTable; import com.google.gwt.user.client.ui.HTML; import com.google.gwt.user.client.ui.HorizontalPanel; import com.google.gwt.user.client.ui.Label; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.TextBox; import com.google.gwt.user.client.ui.VerticalPanel; import com.google.gwt.event.dom.client.KeyCodes; import com.google.gwt.event.dom.client.KeyPressEvent; import com.google.gwt.event.dom.client.KeyPressHandler; /** * Entry point classes define <code>onModuleLoad()</code>. */ public class Rpctest implements EntryPoint { final TextBox firstName = new TextBox(); final TextBox lastName = new TextBox(); final Button ans = new Button("Add User"); //final Label label1 = new Label("First Name"); //final Label label2 = new Label("Last Name"); private FlexTable userFlexTable = new FlexTable(); //final Label errorLabel = new Label(); private VerticalPanel mainpanel = new VerticalPanel(); private HorizontalPanel addpanel1 = new HorizontalPanel(); private HorizontalPanel addpanel2 = new HorizontalPanel(); private final RpctestServiceAsync calNumbers = GWT .create(RpctestService.class); /** * This is the entry point method. */ public void onModuleLoad() { userFlexTable.setText(0, 0, "User ID"); userFlexTable.setText(0, 1, "First Name"); userFlexTable.setText(0, 2, "Second Name"); userFlexTable.setText(0, 3, "Remove"); //add input boxes to panel addpanel1.add(firstName); addpanel1.add(lastName); firstName.setFocus(true); //add input mainpanel.add(userFlexTable); mainpanel.add(addpanel1); addpanel1.add(ans); ans.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { addStock(); } }); lastName.addKeyPressHandler(new KeyPressHandler() { public void onKeyPress(KeyPressEvent event) { if (event.getCharCode() == KeyCodes.KEY_ENTER) { addStock(); } } }); RootPanel.get().add(mainpanel); getUser(); } private void addStock(){ String name1 = firstName.getValue(); // Stock code must be between 1 and 10 chars that are numbers, letters, or dots. /*if (!name1.matches("^[0-9A-Z\\.]{1,10}$")) { Window.alert("'" + name1 + "' is not a valid name."); firstName.selectAll(); return; }*/ firstName.setValue(""); String name2 = lastName.getValue(); /*if (!name2.matches("^[0-9A-Z\\.]{1,10}$")) { Window.alert("'" + name1 + "' is not a valid name."); lastName.selectAll(); return; }*/ lastName.setValue(""); firstName.setFocus(true); calNumbers.addUser(name1,name2, new AsyncCallback<String>() { public void onFailure(Throwable caught) { // Show the RPC error message to the user Window.alert("check your inputs"); } @Override public void onSuccess(String result) { // TODO Auto-generated method stub // Add the user to the table. // int row = userFlexTable.getRowCount(); // userFlexTable.setText(row, 1, result); getUser(); } }); } private void getUser(){ calNumbers.getUser(new AsyncCallback<User[]>() { public void onFailure(Throwable caught) { // Show the RPC error message to the user Window.alert("Problem in database connection"); } @Override public void onSuccess(User[] result) { // TODO Auto-generated method stub for(int i = 0; i < result.length; i ++) { //String s = result[i].getFirstName(); int row = userFlexTable.getRowCount(); userFlexTable.setText(row, 0, result[i].getId().toString()); userFlexTable.setText(row, 1, result[i].getFirstName()); userFlexTable.setText(row, 2, result[i].getLastName()); } } }); } }
您的意思是您的代码工作正常,但您在日志中看到此异常?异常基本上意味着编译缓存由于某种原因无法加载。高速缓存很可能由于某种原因已损坏,因此请尝试从项目中删除文件夹gwt- UnitCache,这应该会有所帮助。