2.1.6
It is one-to-one
User.hbm.xml
<class name="User" table="user" dynamic-update="true" dynamic-insert="true">
<id
column="USER_ID"
name="id"
type="java.lang.Long"
>
<generator class="hilo" />
</id>
<property
column="USER_GRADE"
length="11"
name="userGrade"
not-null="false"
type="integer"
/>
<property
column="USER_NAME"
length="20"
name="userName"
not-null="false"
type="string"
/>
<property
column="USER_PASSWORD"
length="30"
name="userPassword"
not-null="false"
type="string"
/>
<set
inverse="true"
lazy="true"
name="messageSet"
>
<key column="USER_ID" />
<one-to-many class="Message" />
</set>
<set
inverse="true"
lazy="true"
name="articleSet"
>
<key column="USER_ID" />
<one-to-many class="Article" />
</set>
</class>
UserDetail.hbm.xml
<class name="UserDetail" table="user_detail" dynamic-update="true" dynamic-insert="true">
<id
column="USER_DETAIL_ID"
name="id"
type="java.lang.Long"
>
<generator class="foreign" >
<param name="property">user</param>
</generator>
</id>
<property
column="USER_EMAIL"
length="40"
name="userEmail"
not-null="false"
type="string"
/>
..........................................................................
........................................................................
<property
column="PERSON_PICTURE"
name="personPicture"
not-null="false"
type="blob"
/>
<one-to-one name="user" constrained="false"/>
</class>
public User updateUser(User user, UserDetail detail) {
User u=null;
Session s=null;
try {
s=HibernateUtil.currentSession();
s.beginTransaction();
if(null!=user.getId()){
log.info("update --> user.getID is: "+user.getId());
User userinfo=(User)s.get(User.class,user.getId());
try {
BeanUtils.copyProperties(userinfo,user);
BeanUtils.setProperty(userinfo,"id",user.getId());
s.update(userinfo);
u=userinfo;
log.info("u=userinfo id is: "+u.getId());
String detailHQL="from UserDetail detail where detail.user.userName='"+userinfo.getUserName()+"'";
Object odetail=s.createQuery(detailHQL).uniqueResult();
if(null!=odetail){
UserDetail userdetail=(UserDetail)odetail;
log.info("detail info is not NULL! id is :"+userdetail.getId());
BeanUtils.copyProperties(userdetail,detail);
BeanUtils.setProperty(userdetail,"id",userdetail.getId());
log.info("before update["+userdetail.getId()+"]");
s.update(userdetail);
log.info("After update["+userdetail.getId()+"]");
}else{
log.info("ODetail is nULL!");
}
} catch (IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (InvocationTargetException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}else{
log.info("userID is NULL!");
}
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return u;
}
16:15:21,390 ERROR SessionImpl:2379 - Could not synchronize database state with
session
net.sf.hibernate.JDBCException: could not update: [lyo.hotmail.site.bean.User#10
81345]
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.jav
a:707)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.jav
a:642)
at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52
)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2418)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2372)
at net.sf.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.jav
a:1811)
at net.sf.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:1568)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1533)
at net.sf.hibernate.impl.QueryImpl.list(QueryImpl.java:39)
at net.sf.hibernate.impl.AbstractQueryImpl.uniqueResult(AbstractQueryImp
l.java:550)
at lyo.hotmail.site.service.UserDAOImpl.getUser(UserDAOImpl.java:125)
at lyo.hotmail.site.service.UserDAOImpl.getUserDetail(UserDAOImpl.java:2
56)
at lyo.hotmail.site.action.DoUserinfoAction.update(DoUserinfoAction.java
:64)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchActio
n.java:280)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:
216)
at org.apache.struts.action.RequestProcessor.processActionPerform(Reques
tProcessor.java:484)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.ja
va:274)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:148
2)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:237)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:157)
at lyo.hotmail.site.util.HiberFilter.doFilter(HiberFilter.java:48)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:186)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:157)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:214)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:520)
at org.apache.catalina.core.StandardContextValve.invokeInternal(Standard
ContextValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:152)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:104)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(Authentica
torBase.java:462)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:520)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:137)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:104)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:118)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:520)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:109)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:520)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:16
0)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java
:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.proce
ssConnection(Http11Protocol.java:705)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java
:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadP
ool.java:684)
at java.lang.Thread.run(Thread.java:534)
Caused by: java.sql.SQLException: Deadlock found when trying to get lock; Try re
starting transaction message from server: "Lock wait timeout exceeded; Try resta
rting transaction"
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1997)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1167)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1278)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2247)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java
:1772)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java
:1619)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatche
r.java:22)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.jav
a:684)
... 53 more
net.sf.hibernate.JDBCException: could not update: [lyo.hotmail.site.bean.User#10
81345]
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.jav
a:707)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.jav
a:642)
at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52
)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2418)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2372)
at net.sf.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.jav
a:1811)
at net.sf.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:1568)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1533)
at net.sf.hibernate.impl.QueryImpl.list(QueryImpl.java:39)
at net.sf.hibernate.impl.AbstractQueryImpl.uniqueResult(AbstractQueryImp
l.java:550)
at lyo.hotmail.site.service.UserDAOImpl.getUser(UserDAOImpl.java:125)
at lyo.hotmail.site.service.UserDAOImpl.getUserDetail(UserDAOImpl.java:2
56)
at lyo.hotmail.site.action.DoUserinfoAction.update(DoUserinfoAction.java
:64)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchActio
n.java:280)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:
216)
at org.apache.struts.action.RequestProcessor.processActionPerform(Reques
tProcessor.java:484)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.ja
va:274)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:148
2)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:237)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:157)
at lyo.hotmail.site.util.HiberFilter.doFilter(HiberFilter.java:48)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:186)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:157)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:214)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:520)
at org.apache.catalina.core.StandardContextValve.invokeInternal(Standard
ContextValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:152)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:104)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(Authentica
torBase.java:462)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:520)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:137)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:104)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:118)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:520)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:109)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:520)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:16
0)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java
:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.proce
ssConnection(Http11Protocol.java:705)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java
:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadP
ool.java:684)
at java.lang.Thread.run(Thread.java:534)
Caused by: java.sql.SQLException: Deadlock found when trying to get lock; Try re
starting transaction message from server: "Lock wait timeout exceeded; Try resta
rting transaction"
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1997)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1167)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1278)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2247)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java
:1772)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java
:1619)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatche
r.java:22)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.jav
a:684)
... 53 more
MySQL : 3.23.56
Hibernate: update user_detail set PERSON_PICTURE=? where USER_DETAIL_ID=?
INFO:
HI everyone:
I come across a problem now. I want to update a Blob column in MySQL. There is no problem if I update varchar column.But If I update Blob column in the second time,it will Throws Exception:"
java.sql.SQLException: Deadlock found when trying to get lock; Try re
starting transaction message from server: "Lock wait timeout exceeded; Try restarting transaction".
The User and UserDetail is one-to-one accosiation. I commit Transaction and close Session in my servlet Filter:
Code:
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
chain.doFilter(request, response);
try {
HibernateUtil.commitTransaction();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
HibernateUtil.closeSession();
} catch (HibernateException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
The HibernateUtil is a Helper class and I use it create Session and Transaction. The following code :
Code:
public class HibernateUtil {
private static Log log = LogFactory.getLog(HibernateUtil.class);
private static final SessionFactory sessionFactory;
private static final ThreadLocal threadTransaction = new ThreadLocal();
static {
try {
// Create the SessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
log.error("Initial SessionFactory creation failed.", ex);
throw new ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
}
/**
* Start a new database transaction.
*/
public static void beginTransaction()
throws Exception {
Transaction tx = (Transaction) threadTransaction.get();
try {
if (tx == null) {
log.debug("Starting new database transaction in this thread.");
tx = currentSession().beginTransaction();
threadTransaction.set(tx);
}
} catch (HibernateException ex) {
throw new Exception(ex);
}
}
/**
* Commit the database transaction.
*/
public static void commitTransaction()
throws Exception {
Transaction tx = (Transaction) threadTransaction.get();
try {
if ( tx != null && !tx.wasCommitted()
&& !tx.wasRolledBack() ) {
log.debug("Committing database transaction of this thread.");
tx.commit();
}
threadTransaction.set(null);
} catch (HibernateException ex) {
rollbackTransaction();
throw new Exception(ex);
}
}
/**
* Commit the database transaction.
*/
public static void rollbackTransaction()
throws Exception {
Transaction tx = (Transaction) threadTransaction.get();
try {
threadTransaction.set(null);
if ( tx != null && !tx.wasCommitted() && !tx.wasRolledBack() ) {
log.debug("Tyring to rollback database transaction of this thread.");
tx.rollback();
}
} catch (HibernateException ex) {
throw new Exception(ex);
} finally {
closeSession();
}
}
My Servlet is:
Code:
public ActionForward update(ActionMapping arg0, ActionForm arg1,
HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
log.info("Enter updateUser action!");
UpdateUserForm form=(UpdateUserForm)arg1;
log.info("form is : "+form);
log.info("Get username: "+form.getUserName());
User user=new User();
UserDetail detail=new UserDetail();
BeanUtils.copyProperties(user,form);
BeanUtils.copyProperties(detail,form);
log.info("get formFile: "+form.getPersonPictureFile());
if(null!=form.getPersonPictureFile()&&form.getPersonPictureFile().getFileSize()!=0){
detail.setPersonPicture(Hibernate.createBlob(form.getPersonPictureFile().getInputStream()));
}
/*** create user ***/
UserDAO udao=new UserDAOImpl();
User returnuser=udao.updateUser(user,detail);
if(null==returnuser){
log.info("returnuser is NULL!");
arg2.setAttribute("error.msg","update error,already has this user!");
return arg0.findForward("error");
}
UserDetail userdetail=udao.getUserDetail(returnuser.getUserName());
HttpSession session=arg2.getSession();
arg2.setAttribute("detail",userdetail);
session.setAttribute("user",returnuser);
return arg0.findForward("success");
}
Why I can't update Blob column. What I want to do is update a picture in MySQL. Why database will dealLock when I update the picture column?
:(
Help!