I have a script that runs for hours sending out large email campaigns..
I have a table called "Email" and a table called "Sent" there is a FK in sent that references the email table..
I have a large for loop, that loops thru all the contacts the email is to be sent to, it creates a new Sent entity for every email that is sent
The problem I am having is that currently I commit the transaction every 100 emails.. which can take a few minutes to do.. and during that time the email row is locked.. is there any way to make hibernate not lock that row?
Here is some sample code to make this more clear?? hopefully?
Code:
private void process(Email email, List<Contact> contacts)
{
et.begin();
int counter = 0;
for (Contact contact : contacts)
{
Sent sent = new Sent();
sent.setEmail(email);
sent.setUuid(UUID.randomUUID().toString();
sent.setDate(new Date());
sent.setContact(contact);
em.persist(sent);
// this uses the uuid and contact from the sent entity
send(email, sent);
counter++;
if (counter % 100 == 0)
{
et.commit();
et.begin();
}
}
et.commit();
}