Skip to content
This repository has been archived by the owner on Jan 5, 2023. It is now read-only.

Commit

Permalink
Add test for allocation size when inserting using sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
vladmihalcea committed Jul 29, 2015
1 parent 132fd5c commit a8809bc
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 5 deletions.
Expand Up @@ -3,18 +3,43 @@
import com.vladmihalcea.book.high_performance_java_persistence.jdbc.batch.providers.SequenceBatchEntityProvider;
import com.vladmihalcea.hibernate.masterclass.laboratory.util.AbstractTest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.sql.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
* AbstractSequenceGeneratedKeysBatchPreparedStatementTest - Base class for testing JDBC PreparedStatement generated keys for Sequences
*
* @author Vlad Mihalcea
*/
@RunWith(Parameterized.class)
public abstract class AbstractSequenceGeneratedKeysBatchPreparedStatementTest extends AbstractTest {

private SequenceBatchEntityProvider entityProvider = new SequenceBatchEntityProvider();

private int allocationSize = 1;

public AbstractSequenceGeneratedKeysBatchPreparedStatementTest(int allocationSize) {
this.allocationSize = allocationSize;
}

@Parameterized.Parameters
public static Collection<Integer[]> rdbmsDataSourceProvider() {
List<Integer[]> providers = new ArrayList<>();
providers.add(new Integer[]{1});
providers.add(new Integer[]{5});
providers.add(new Integer[]{10});
providers.add(new Integer[]{15});
providers.add(new Integer[]{20});
providers.add(new Integer[]{25});
return providers;
}

@Override
protected Class<?>[] entities() {
return entityProvider.entities();
Expand All @@ -26,15 +51,15 @@ public void testBatch() {
}

protected int getPostCount() {
return 10;
return 5 * 1000;
}

protected int getBatchSize() {
return 25;
}

protected int getAllocationSize() {
return 1;
return allocationSize;
}

protected void batchInsert(Connection connection) throws SQLException {
Expand All @@ -46,6 +71,8 @@ protected void batchInsert(Connection connection) throws SQLException {

int count = 0;

long startNanos = System.nanoTime();

try(PreparedStatement postStatement = connection.prepareStatement(
"insert into Post (id, title, version) " +
"values (?, ?, ?)")) {
Expand All @@ -61,7 +88,6 @@ protected void batchInsert(Connection connection) throws SQLException {
callSequenceSyntax())) {
resultSet.next();
id = resultSet.getLong(1);
LOGGER.info("Generated id {}", id);
}
}

Expand All @@ -75,6 +101,12 @@ protected void batchInsert(Connection connection) throws SQLException {
}
postStatement.executeBatch();
}

LOGGER.info("{}.testInsert for {} using allocation size {} took {} millis",
getClass().getSimpleName(),
getDataSourceProvider().getClass().getSimpleName(),
getAllocationSize(),
TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos));
}

protected abstract String callSequenceSyntax();
Expand Down
Expand Up @@ -7,6 +7,10 @@
*/
public class OracleSequenceGeneratedKeysBatchPreparedStatementTest extends AbstractSequenceGeneratedKeysBatchPreparedStatementTest {

public OracleSequenceGeneratedKeysBatchPreparedStatementTest(int allocationSize) {
super(allocationSize);
}

@Override
protected String callSequenceSyntax() {
return "select post_seq.NEXTVAL from dual";
Expand Down
@@ -1,12 +1,16 @@
package com.vladmihalcea.book.high_performance_java_persistence.jdbc.batch.generatedkeys.sequence;

/**
* GeneratedKeysBatchPreparedStatementTest - PostgreSQL class for testing JDBC PreparedStatement generated keys for Sequences
* PostgreSQLSequenceGeneratedKeysBatchPreparedStatementTest - PostgreSQL class for testing JDBC PreparedStatement generated keys for Sequences
*
* @author Vlad Mihalcea
*/
public class PostgreSQLSequenceGeneratedKeysBatchPreparedStatementTest extends AbstractSequenceGeneratedKeysBatchPreparedStatementTest {

public PostgreSQLSequenceGeneratedKeysBatchPreparedStatementTest(int allocationSize) {
super(allocationSize);
}

@Override
protected String callSequenceSyntax() {
return "select nextval('post_seq')";
Expand Down
Expand Up @@ -7,6 +7,10 @@
*/
public class SQLServerSequenceGeneratedKeysBatchPreparedStatementTest extends AbstractSequenceGeneratedKeysBatchPreparedStatementTest {

public SQLServerSequenceGeneratedKeysBatchPreparedStatementTest(int allocationSize) {
super(allocationSize);
}

@Override
protected String callSequenceSyntax() {
return "select NEXT VALUE FOR post_seq";
Expand Down
2 changes: 1 addition & 1 deletion core/src/test/resources/logback.xml
Expand Up @@ -22,7 +22,7 @@

<logger name="org.springframework" level="warn"/>

<logger name="net.ttddyy.dsproxy.listener" level="debug"/>
<logger name="net.ttddyy.dsproxy.listener" level="info"/>

<logger name="org.hibernate" level="warn"/>
<logger name="org.hibernate.event.internal" level="info"/>
Expand Down

0 comments on commit a8809bc

Please sign in to comment.