Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » @TableGenerator - Pre-allocate ID block not work fine [SOLVED](More persistence in the same transaction skip the entire id's block)
icon14.gif  @TableGenerator - Pre-allocate ID block not work fine [SOLVED] [message #1320011] Mon, 28 April 2014 14:32 Go to next message
Antonio Iovino is currently offline Antonio IovinoFriend
Messages: 3
Registered: April 2014
Junior Member
Hi at all.
I'm trying to manage the ID generation through the annotation @TableGenerator with allocationSize default. As I understand it, to avoid updating the row for every single identifier that gets requested, an allocation size is used. In theory, the provider should pre-allocate a block of identifiers equal to the value of allocationSize - 50 in this case - and then give out identifiers from memory as requested until the block is used up or the transaction comes to an end. I use Eclipselink with JBoss 7.1 and MySQL.

The problem is that this does not happen. Inserting 3 records in the table Students, EL pre allocates for each record a block of 50 ID, even if the transaction is the same. Then, for each record, there is always access to the table. From the logs I see 3 pre allocations and three pairs of select/update query for the ID and the IDs are generated 1- 51 -101 and the sequence has as its final value 150. Piece of log.

Being a single transaction, I expected sequential IDs (1-2-3) and the final value of the sequence 50. Where am I wrong? Below is the simple test code.
Thanks for the help.

Student Entity
@Entity
@Table(name="STUDENTS")
public class Student implements Serializable
{
    private static final long serialVersionUID = 4771385985502937621L;

    @TableGenerator(name="TABLE_SEQ")
    @Id @Column(name="ID_STUDENT") @GeneratedValue(generator="TABLE_SEQ")
    private int idStudent;

    private String name;

    public int getIdStudent() {
        return idStudent;
    }

    public void setIdStudent(int idStudent) {
        this.idStudent = idStudent;
    }

    public String getName() {
        return name;
    }

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


Stateless EJB
@Stateless(name="EJBStudent")
public class EJBStudent implements EJBStudentRemote
{
    @PersistenceContext(unitName="JPA_Test")
    private EntityManager manager;

    public EJBStudent() {
    }


    @Override
    public void insertStudents() 
    {
        manager.getTransaction().begin();

        Student student1 = new Student();
        student1.setName("Anna");
        manager.persist(student1);

        Student student2 = new Student();
        student2.setName("Paolo");
        manager.persist(student2);

        Student student3 = new Student();
        student3.setName("Luigi");
        manager.persist(student3);

        manager.getTransaction().commit();
    }
}


I tried to replace the commit() with flush(), but nothing changes.

[Updated on: Mon, 05 May 2014 15:40]

Report message to a moderator

Re: @TableGenerator - Pre-allocate ID block not work fine [message #1324082 is a reply to message #1320011] Wed, 30 April 2014 15:54 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
What does your persistence.xml look like, and what version of EclipseLink are you using? This looks like a bug, so you might file it with the details and a test case required to reproduce it and try the latest EclipseLink version if you aren't already.

Best Regards,
Chris
Re: @TableGenerator - Pre-allocate ID block not work fine [message #1333912 is a reply to message #1320011] Mon, 05 May 2014 09:44 Go to previous messageGo to next message
Antonio Iovino is currently offline Antonio IovinoFriend
Messages: 3
Registered: April 2014
Junior Member
Hi Chris,
i use the latest release and this is the persistence.cml
(I deleted the links in the namespace otherwise I could not post the message)

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="..." xmlns:xsi="..." xsi:schemaLocation="...">
		
    <persistence-unit name="JPA_Test">		
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <jta-data-source>java:jboss/datasources/MySqlDS</jta-data-source>
        <class>jpa.test.model.Students</class>
	
        <properties>
            <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
            <property name="eclipselink.logging.level" value="FINER"/>
            <property name="eclipselink.logging.parameters" value="true" />
            <property name="eclipselink.deploy-on-startup" value="true" />
        </properties>
    </persistence-unit>
</persistence>

Honestly I do not think this is a bug, I'll explain why.I noticed a strange behavior very that makes it all work properly.
If after the start of the transaction I execute a query, for example:

select * from SEQUENCE where SEQ_NAME = 'TABLE_SEQ'

the code works perfectly. The records are recorded with sequential IDs (1, 2, 3) and the counter in the table is set to 50.
This new code:

@Override
    public void insertStudents() 
    {
        manager.getTransaction().begin();
	Query query = manager.createNativeQuery("select * from SEQUENCE where SEQ_NAME = 'TABLE_SEQ'");
	query.getResultList();

        Student student1 = new Student();
        /* ... code for persist three student ... */

        manager.getTransaction().commit();
    }


Maybe I'm wrong, but it is as if there is a problem of connection or session.
Any idea?
Thanks.
Re: @TableGenerator - Pre-allocate ID block not work fine [message #1334275 is a reply to message #1333912] Mon, 05 May 2014 13:41 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
Looks like you are missing the target server property that might help hook EclipseLink into the JBoss transaction controller:
<property name="eclipselink.target-server" value="JBoss"/>

Without it, EclipseLink might not be correctly hooking into the transaction.

Best Regards,
Chris
Re: @TableGenerator - Pre-allocate ID block not work fine [message #1334444 is a reply to message #1334275] Mon, 05 May 2014 15:39 Go to previous message
Antonio Iovino is currently offline Antonio IovinoFriend
Messages: 3
Registered: April 2014
Junior Member
Great chris!
You're right, great Very Happy. Problem solved, thank you very very much!

The only difference is that now I deleted from the code the instructions getTransaction().begin and commit(), otherwise it was in error.
I imagine that it has acted in this way for the reason that you have explained, that is EL now knows how to treat transactions.
Thanks again.
Previous Topic:Unmapped Columns in ResultSet
Next Topic:Transactions
Goto Forum:
  


Current Time: Thu Apr 18 12:51:05 GMT 2024

Powered by FUDForum. Page generated in 0.02233 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top