Hi again,

I tried @TransactionAnnotation but it generated frequent errors like this :

[10:44:42.851] {http--8000-2} error: cannot read: mp/adminservlet.java
[10:44:42.851] {http--8000-2} 1 error

the select still work but no update


I tried then with the UserTransaction method, but there was no update. I looked at the amber-basic-field example, and found that even if the _uTrans was defined, it was not used, instead I found _manager.getTransaction(), so I tried with that, and still no update.

but now I can see in the finer log for sql :

[11:18:22.968] {http--8000-10} mp_db_pool.0:setAutoCommit(false)
[11:18:22.968] {http--8000-10} mp_db_pool.0:commit()
[11:18:22.969] {http--8000-10} mp_db_pool.0:setAutoCommit(true)

Which means that the transaction is being used, but no update when doing setTitle()

I must be missing something, because the sample works all right. I re-read all conf, and found nothing strange. The only difference seems to be that I use jdbc to mysql instead of "jdbc/resin" embedded db.

Thanks for any info.
Attached some files of my little test.

Scott Ferguson wrote:
On Mar 10, 2008, at 1:29 AM, Riccardo Cohen wrote:

Thanks a lot, I understand that I need a transaction now, while before
it could work without this... I have no idea of how to do that.

I noticed that in the doc of amber
(http://caucho.com/resin/doc/amber.xtp) there was a
@TransactionAnnotation but this makes a syntax error !
Thanks to eclipse I found a @TransactionAnn, I added it at the beginning of my function definition, but this does not change (no update generated)

It should be @TransactionAttribute.  I'm fixing the docs.

The @TransactionAttribute is the easiest method. You need to put it on a Resin-IoC-aware object, e.g. a <bean> or an EJB stateless bean or a servlet. If you put it on an arbitrary class, it won't do anything.

You can also use UserTransaction to do essentially the same thing:

class MyFoo {
   @In UserTransaction _ut;

   void myStuff()
   {
     _ut.begin();
     try {
       ...
     } finally {
       ut.commit();
     }
  }

That's essentially identical to

   @TransactionAttribute void myStuff()
   {
     ...
   }

If you turn logging level="fine", you'll see the transaction begin()/ commit(). So you can use the log to make sure the transactions are working properly.

-- Scott

Is there any doc about this ?
Thanks

Daniel López wrote:
Hi again,

Some quick tests show that no persist() or merge() should be necessary to update an entity inside a persistent context. I tested with Hibernate
and Amber as persistence providers and in both cases, nothing was
necessary. That was using Resin 3.1.5 and RESOURCE_LOCAL as transaction type, which means that the problem might be with the container managed
transactions.

S!
D.

Daniel López escribió:
AFAIK, using merge should not be necessary unless the entity has been updated outside a "persistent context" and then needs to be synchronised back with the DB contents. persist() is just for new entities so reading the docs, updating an entity inside a persistent context should require
no action. Unless an exception is thrown, of course ;).

I'm going to do some tests...

S!
D.

Matt Johnston escribió:
I think you will need to use either the persist() or merge() methods of the EntityManager in order to save your data to the database. In your case since you are updating an existing record, you will need to use:

m_manager.merge(homeobj)


Matt

Riccardo Cohen wrote:
Hi
I used to play with entity ejb with resin 3.0 with no problem. Now in
3.1.5 I have this code :

@PersistenceContext(name="public") private EntityManager m_manager;

  public boolean set_homeinfo(int id_user,String title)
  {
    boolean success=false;
Query hqr=m_manager.createQuery("select h from homeinfo h where
h.id_user="+id_user);
    List<homeinfo> hitems = (List<homeinfo>)hqr.getResultList();
    if (hitems.size()==1)
    {
      homeinfo homeobj=hitems.get(0);
      System.out.println("title was "+homeobj.getTitle());
      homeobj.setTitle(title);
      success=true;
    }
    return(success);
  }

The select works all right, but the "title" field is never modified. I added finer info on sql to see database requests in log, and there is no
"update". Did I miss something ?

I looked at the resin amber tutorials, but there are only "select"
samples, I did not see "insert" and "update" samples... I remember
problems like this with 3.0 when the entity bean was reused, it was not
saved, but here it is not the case.

Thanks for any help.


_______________________________________________
resin-interest mailing list
resin-interest@caucho.com
http://maillist.caucho.com/mailman/listinfo/resin-interest

--
Très cordialement,

Riccardo Cohen
-------------------------------------------
Articque
http://www.articque.com
149 av Général de Gaulle
37230 Fondettes - France
tel : 02-47-49-90-49
fax : 02-47-49-91-49


_______________________________________________
resin-interest mailing list
resin-interest@caucho.com
http://maillist.caucho.com/mailman/listinfo/resin-interest



_______________________________________________
resin-interest mailing list
resin-interest@caucho.com
http://maillist.caucho.com/mailman/listinfo/resin-interest


--
Très cordialement,

Riccardo Cohen
-------------------------------------------
Articque
http://www.articque.com
149 av Général de Gaulle
37230 Fondettes - France
tel : 02-47-49-90-49
fax : 02-47-49-91-49
<web-app xmlns="http://caucho.com/ns/resin";>
  <database>
    <jndi-name>mp_db_pool</jndi-name>
    <driver>
      <type>org.gjt.mm.mysql.Driver</type>
      <url>jdbc:mysql://localhost/presencemusicale</url>
      <init-param autoReconnect="true"/>
      <init-param noDatetimeStringSync="true"/>
      <init-param jdbcCompliantTruncation="false"/>
      <user>mp</user>
      <password>mp</password>
    </driver>
    <prepared-statement-cache-size>8</prepared-statement-cache-size>
    <max-connections>20</max-connections>
    <max-idle-time>30s</max-idle-time>
    <spy>true</spy>
  </database>
  <ejb-server data-source="mp_db_pool" create-database-schema="false"/>


  <servlet servlet-name="public" servlet-class="mp.publicaction">
    <load-on-startup/>
  </servlet>
  <servlet-mapping url-pattern="/public" servlet-name="public"/>

  <servlet servlet-name="admin" servlet-class="mp.adminaction">
    <load-on-startup/>
  </servlet>
  <servlet-mapping url-pattern="/admin" servlet-name="admin"/>


  <servlet servlet-name="adminhessian" servlet-class="mp.adminservlet"/>
  <servlet-mapping url-pattern="/adminhessian/*" servlet-name="adminhessian"/>

</web-app>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"; version="1.0">
  <persistence-unit name="public">
    <class>mpentities.country</class>
    <class>mpentities.hit</class>
    <class>mpentities.homeinfo</class>
    <class>mpentities.instrument</class>
    <class>mpentities.musicstyle</class>
    <class>mpentities.place</class>
    <class>mpentities.recommendation</class>
    <class>mpentities.region</class>
    <class>mpentities.school</class>
    <class>mpentities.schoolcredit</class>
    <class>mpentities.show</class>
    <class>mpentities.showinstrument</class>
    <class>mpentities.user</class>
    <class>mpentities.usercredit</class>
    <class>mpentities.userinstrument</class>
    <class>mpentities.usermedia</class>
    <class>mpentities.usershow</class>
    <class>mpentities.userstyle</class>
    <exclude-unlisted-classes />
  </persistence-unit>
</persistence>
package mp;

import java.util.*;
import javax.persistence.*;
import javax.transaction.*;
import mpentities.*;
import com.caucho.hessian.server.*;
import javax.webbeans.In;

public class adminservlet extends HessianServlet implements admininterf
{
  @PersistenceContext(name="public") private EntityManager m_manager;
  @In private UserTransaction m_ut;
  
  //@TransactionAttribute 
  public boolean set_homeinfo(int id_user,String title)
  {
    boolean success=false;
    Query hqr=m_manager.createQuery("select h from homeinfo h where 
h.id_user="+id_user);
    List<homeinfo> hitems = (List<homeinfo>)hqr.getResultList();
    if (hitems.size()==1)
    {
      homeinfo homeobj=hitems.get(0);
      System.out.println("title was "+homeobj.getTitle());
      try 
      {
        m_ut.begin();
        homeobj.setTitle(title);
        success=true;
      }
      catch(Exception ex){ex.printStackTrace();}
      finally 
      {
        try{m_ut.commit();}catch(Exception ex){ex.printStackTrace();}
      }
    }
    return(success);
  }

  public user getuser(int id)
  {
    user ret=null;
    user obj=(user)m_manager.find(user.class,id);
    if (obj!=null)
    {
      ret=obj;
      System.out.println("send "+obj.getName());
    }
    else
    {
      ret=new user();
      ret.setId(-1);
      System.out.println("send -1");
    }
    return ret;
  }

}
package mpentities;

import javax.persistence.*;

@Entity
@Table(name="homeinfo")
public class homeinfo 
{
  @Id @Column(name="id") @GeneratedValue int id;
  @Basic @Column(name="id_user") int id_user;
  @Basic @Column(name="title") String title;
  @Basic @Column(name="subtitle") String subtitle;
  @Basic @Column(name="hometext") String hometext;
  @Basic @Column(name="backcolor") String backcolor;
  @Basic @Column(name="textcolor") String textcolor;
  public int getId()
  {
    return id;
  }
  public void setId(int id)
  {
    this.id = id;
  }
  public int getId_user()
  {
    return id_user;
  }
  public void setId_user(int id_user)
  {
    this.id_user = id_user;
  }
  public String getTitle()
  {
    return title;
  }
  public void setTitle(String title)
  {
    this.title = title;
  }
  public String getSubtitle()
  {
    return subtitle;
  }
  public void setSubtitle(String subtitle)
  {
    this.subtitle = subtitle;
  }
  public String getHometext()
  {
    return hometext;
  }
  public void setHometext(String hometext)
  {
    this.hometext = hometext;
  }
  public String getBackcolor()
  {
    return backcolor;
  }
  public void setBackcolor(String backcolor)
  {
    this.backcolor = backcolor;
  }
  public String getTextcolor()
  {
    return textcolor;
  }
  public void setTextcolor(String textcolor)
  {
    this.textcolor = textcolor;
  }
}
_______________________________________________
resin-interest mailing list
resin-interest@caucho.com
http://maillist.caucho.com/mailman/listinfo/resin-interest

Reply via email to