0

I'm getting following logs when I attempt to add list of my objects into database.

=> https://api.twitter.com/1.1/trends/place.json?id=1
Try to store into database...
=> 0 - TwitterTrendsJPA{id=null, name='#7YearsOfKidrauhl', url='http://twitter.com/search?q=%237YearsOfKidrauhl', query='%237YearsOfKidrauhl', location='Worldwide', woeid=1}
=> 1 - TwitterTrendsJPA{id=null, name='#DisneyMarvelDiAlfamart', url='http://twitter.com/search?q=%23DisneyMarvelDiAlfamart', query='%23DisneyMarvelDiAlfamart', location='Worldwide', woeid=1}
=> 2 - TwitterTrendsJPA{id=null, name='#KnockOffBands', url='http://twitter.com/search?q=%23KnockOffBands', query='%23KnockOffBands', location='Worldwide', woeid=1}
=> 3 - TwitterTrendsJPA{id=null, name='#JKT48PeduliSinabung', url='http://twitter.com/search?q=%23JKT48PeduliSinabung', query='%23JKT48PeduliSinabung', location='Worldwide', woeid=1}
=> 4 - TwitterTrendsJPA{id=null, name='#Yolsuzlu?uGizleHerkesiDinle', url='http://twitter.com/search?q=%23Yolsuzlu%C4%9FuGizleHerkesiDinle', query='%23Yolsuzlu%C4%9FuGizleHerkesiDinle', location='Worldwide', woeid=1}
=> 5 - TwitterTrendsJPA{id=null, name='GALATASARAYSevgisi SürecekSonsuzaDek', url='http://twitter.com/search?q=%22GALATASARAYSevgisi+S%C3%BCrecekSonsuzaDek%22', query='%22GALATASARAYSevgisi+S%C3%BCrecekSonsuzaDek%22', location='Worldwide', woeid=1}
=> 6 - TwitterTrendsJPA{id=null, name='Juan Gelman', url='http://twitter.com/search?q=%22Juan+Gelman%22', query='%22Juan+Gelman%22', location='Worldwide', woeid=1}
=> 7 - TwitterTrendsJPA{id=null, name='Radio Galau FM', url='http://twitter.com/search?q=%22Radio+Galau+FM%22', query='%22Radio+Galau+FM%22', location='Worldwide', woeid=1}
=> 8 - TwitterTrendsJPA{id=null, name='Australian Open', url='http://twitter.com/search?q=%22Australian+Open%22', query='%22Australian+Open%22', location='Worldwide', woeid=1}
=> 9 - TwitterTrendsJPA{id=null, name='Traveling', url='http://twitter.com/search?q=Traveling', query='Traveling', location='Worldwide', woeid=1}
Jan 15, 2014 3:40:31 PM com.google.appengine.api.datastore.dev.LocalDatastoreService init
INFO: Local Datastore initialized: 
    Type: High Replication
    Storage: D:\Android\IntelliJ IDEA\workspace\EyeBall\out\artifacts\EyeBall_war_exploded\WEB-INF\appengine-generated\local_db.bin
Jan 15, 2014 3:40:31 PM com.google.appengine.api.datastore.dev.LocalDatastoreService load
INFO: Time to load datastore: 29 ms
Jan 15, 2014 3:40:32 PM com.google.apphosting.utils.servlet.TransactionCleanupFilter handleAbandonedTxns
WARNING: Request completed without committing or rolling back transaction with id 0.  Transaction will be rolled back.

At first I'm sending a request to Twitter in order to pass me list of trends. After that I parse json string and create TwitterTrendsJPA object. This is my class:

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;


@Entity
public class TwitterTrendsJPA {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;
    private String url;
    private String query;
    private String location;
    private int woeid;

    public TwitterTrendsJPA() {

    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getQuery() {
        return query;
    }

    public void setQuery(String query) {
        this.query = query;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public int getWoeid() {
        return woeid;
    }

    public void setWoeid(int woeid) {
        this.woeid = woeid;
    }

    @Override
    public String toString() {
        return "TwitterTrendsJPA{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", url='" + url + '\'' +
                ", query='" + query + '\'' +
                ", location='" + location + '\'' +
                ", woeid=" + woeid +
                '}';
    }
}

Then in my service, I'm asking EntityManager to put it into database.

System.out.println("Try to store into database...");
EntityManager em = EMF.get().createEntityManager();
    try {
        for (int i=0; i<twitterTrendsJPAs.size(); i++) {
                        System.out.println("=> " + i + " - " + twitterTrendsJPAs.get(i).toString());
        em.persist(twitterTrendsJPAs.get(i)); // store current one
        }
    } finally {
        em.close();
    }
     System.out.println("wow. Finished.");

I have designed this process based on this document. Based on my test it's possible to store just one item but when I put it in loop the doesn't work. for example if I replace for (int i=0; i<twitterTrendsJPAs.size(); i++) {...} with for (int i=0; i<1; i++) {...} application is able to store first item into database.

Any suggestion would be appreciated.

Lipis
  • 21,388
  • 20
  • 94
  • 121
Hesam
  • 52,260
  • 74
  • 224
  • 365
  • 1
    Why don't you open a transaction and at the end commit it? – V G Jan 15 '14 at 09:04
  • Thanks Andrei, I diged the Internet actually but didn't find something helpful. Do you have any good reference? – Hesam Jan 15 '14 at 10:04
  • 1
    Look in this question (not answer): http://stackoverflow.com/questions/8464370/jpa-when-to-use-gettransaction-when-persisting-objects Also: are you sure that the error comes from that persisting code? – V G Jan 15 '14 at 10:10

1 Answers1

0

The problem solved by this way:

for (int i=0; i<twitterTrendsJPAs.size(); i++) {
    EntityManager em = EMF.get().createEntityManager();
    try {
        System.out.println("=> " + i + " - " + twitterTrendsJPAs.get(i).toString());
        em.persist(twitterTrendsJPAs.get(i)); // store current one

    } finally {
        if (em != null)
            em.close();
    }
}

I believe this is horrible way since I'm making "em" several time and mark it to be garbage collected. However, previous idea which was a better way didn't work.

Any better idea would be appreciated. Thanks.

===========================

Update: Thanks to DataNucleus. I changed first code based on what he said and it's now working fine.

System.out.println("Try to store into database...");
EntityManager em = EMF.get().createEntityManager();
    try {
        for (int i=0; i<twitterTrendsJPAs.size(); i++) {
            System.out.println("=> " + i + " - " + twitterTrendsJPAs.get(i).toString());
            em.getTransaction().begin();
            em.persist(twitterTrendsJPAs.get(i)); // store current one
            em.getTransaction().commit();
        }
    } finally {
        em.close();
    }
System.out.println("wow. Finished.");
Hesam
  • 52,260
  • 74
  • 224
  • 365
  • 1
    That's not a "solution". It's a hack that doesn't look at why you have a problem. The log tells you much more about what is happening (DEBUG level), so suggest you look at it. It was suggested you put a transaction in there above, so why not do that. em.getTransaction().begin(), then em.getTransaction().commit() after it - i.e basic JPA – DataNucleus Jan 15 '14 at 11:05