Skip to content

Commit c344424

Browse files
committed
Refactor Method Signature
Throwing out all checked exceptions.
1 parent d1d9116 commit c344424

9 files changed

Lines changed: 58 additions & 19 deletions

File tree

JSqlServerBulkInsert/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
<groupId>de.bytefish</groupId>
99
<artifactId>jsqlserverbulkinsert</artifactId>
10-
<version>1.0</version>
10+
<version>1.1</version>
1111
<name>jsqlserverbulkinsert</name>
1212
<description>JSqlServerBulkInsert is a Java library for Bulk Inserts to the SQL Server.</description>
1313
<url>http://www.github.com/bytefish/JSqlServerBulkInsert</url>

JSqlServerBulkInsert/src/main/java/de/bytefish/jsqlserverbulkinsert/SqlServerBulkInsert.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.microsoft.sqlserver.jdbc.ISQLServerBulkRecord;
77
import com.microsoft.sqlserver.jdbc.SQLServerBulkCopy;
88
import com.microsoft.sqlserver.jdbc.SQLServerBulkCopyOptions;
9+
import com.microsoft.sqlserver.jdbc.SQLServerException;
910
import de.bytefish.jsqlserverbulkinsert.mapping.AbstractMapping;
1011
import de.bytefish.jsqlserverbulkinsert.records.SqlServerRecord;
1112

@@ -22,11 +23,11 @@ public SqlServerBulkInsert(AbstractMapping<TEntity> mapping)
2223
this.mapping = mapping;
2324
}
2425

25-
public void saveAll(Connection connection, Stream<TEntity> entities) throws SQLException {
26+
public void saveAll(Connection connection, Stream<TEntity> entities) {
2627
saveAll(connection, new SQLServerBulkCopyOptions(), entities);
2728
}
2829

29-
public void saveAll(Connection connection, SQLServerBulkCopyOptions options, Stream<TEntity> entities) throws SQLException {
30+
public void saveAll(Connection connection, SQLServerBulkCopyOptions options, Stream<TEntity> entities) {
3031
// Create a new SQLServerBulkCopy Instance on the given Connection:
3132
try (SQLServerBulkCopy sqlServerBulkCopy = new SQLServerBulkCopy(connection)) {
3233
// Set the Options:
@@ -36,14 +37,10 @@ public void saveAll(Connection connection, SQLServerBulkCopyOptions options, Str
3637
// The SQL Records to insert:
3738
ISQLServerBulkRecord record = new SqlServerRecord<TEntity>(mapping.getColumns(), entities.iterator());
3839
// Finally start the Bulk Copy Process:
39-
internalWriteToServer(sqlServerBulkCopy, record);
40-
}
41-
}
42-
43-
public void internalWriteToServer(SQLServerBulkCopy sqlServerBulkCopy, ISQLServerBulkRecord record) {
44-
try {
4540
sqlServerBulkCopy.writeToServer(record);
46-
} catch(Exception e) {
41+
// Handle Exceptions:
42+
} catch (SQLServerException e) {
43+
// Wrap it in a RunTimeException to provide a nice API:
4744
throw new RuntimeException(e);
4845
}
4946
}

JSqlServerBulkInsert/src/main/java/de/bytefish/jsqlserverbulkinsert/functional/Action0.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55

66
@FunctionalInterface
77
public interface Action0 {
8-
void invoke() throws Exception;
8+
void invoke();
99
}

JSqlServerBulkInsert/src/main/java/de/bytefish/jsqlserverbulkinsert/functional/Action1.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55

66
@FunctionalInterface
77
public interface Action1<S> {
8-
void invoke(S s) throws Exception;
8+
void invoke(S s);
99
}

JSqlServerBulkInsert/src/main/java/de/bytefish/jsqlserverbulkinsert/functional/Action2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55

66
@FunctionalInterface
77
public interface Action2<S, T> {
8-
void invoke(S s, T t) throws Exception;
8+
void invoke(S s, T t);
99
}

JSqlServerBulkInsert/src/main/java/de/bytefish/jsqlserverbulkinsert/functional/Func1.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55

66
@FunctionalInterface
77
public interface Func1<S> {
8-
S invoke() throws Exception;
8+
S invoke();
99
}

JSqlServerBulkInsert/src/test/java/de/bytefish/jsqlserverbulkinsert/test/integration/IntegrationTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import de.bytefish.jsqlserverbulkinsert.test.model.Person;
88
import de.bytefish.jsqlserverbulkinsert.SqlServerBulkInsert;
99
import de.bytefish.jsqlserverbulkinsert.test.base.TransactionalTestBase;
10+
import de.bytefish.jsqlserverbulkinsert.test.utils.MeasurementUtils;
1011
import org.junit.Assert;
1112
import org.junit.Test;
1213

@@ -34,8 +35,11 @@ public void bulkInsertPersonDataTest() throws SQLException {
3435
PersonMapping mapping = new PersonMapping();
3536
// Create the Bulk Inserter:
3637
SqlServerBulkInsert<Person> bulkInsert = new SqlServerBulkInsert<>(mapping);
37-
// Now save all entities of a given stream:
38-
bulkInsert.saveAll(connection, persons.stream());
38+
// Measure the Bulk Insert time:
39+
MeasurementUtils.MeasureElapsedTime("Bulk Insert 1000000 Entities", () -> {
40+
// Now save all entities of a given stream:
41+
bulkInsert.saveAll(connection, persons.stream());
42+
});
3943
// And assert all have been written to the database:
4044
Assert.assertEquals(numEntities, getRowCount());
4145
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package de.bytefish.jsqlserverbulkinsert.test.utils;
2+
3+
import de.bytefish.jsqlserverbulkinsert.functional.Action0;
4+
5+
import java.time.Duration;
6+
import java.time.Instant;
7+
8+
public class MeasurementUtils {
9+
10+
public static void MeasureElapsedTime(String description, Action0 action) {
11+
Duration duration = MeasureElapsedTime(action);
12+
13+
System.out.println(String.format("[%s] %s", description, duration));
14+
}
15+
16+
private static Duration MeasureElapsedTime(Action0 action) {
17+
Instant start = Instant.now();
18+
19+
action.invoke();
20+
21+
Instant end = Instant.now();
22+
23+
return Duration.between(start, end);
24+
}
25+
}

README.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# JSqlServerBulkInsert #
22

3+
[JSqlServerBulkInsert]: https://github.com/bytefish/JSqlServerBulkInsert
34
[MIT License]: https://opensource.org/licenses/MIT
45

56
[JSqlServerBulkInsert] is a library to simplify Bulk Inserts to the SQL Server. It wraps the ``SQLServerBulkCopy`` behind a nice API.
@@ -12,14 +13,22 @@ You can obtain [JSqlServerBulkInsert] from Maven by adding the following:
1213
<dependency>
1314
<groupId>de.bytefish</groupId>
1415
<artifactId>jsqlserverbulkinsert</artifactId>
15-
<version>1.0</version>
16+
<version>1.1</version>
1617
</dependency>
1718
```
1819

1920
## Getting Started ##
2021

2122
Imagine ``1,000,000`` Persons should be inserted into an SQL Server database.
2223

24+
### Results ###
25+
26+
Bulk Inserting ``1,000,000``entities to a SQL Server 2016 database took ``5`` Seconds:
27+
28+
```
29+
[Bulk Insert 1000000 Entities] PT4.559S
30+
```
31+
2332
### Domain Model ###
2433

2534
The domain model could be the ``Person`` class with a First Name, Last Name and a birth date.
@@ -118,6 +127,7 @@ import de.bytefish.jsqlserverbulkinsert.mapping.AbstractMapping;
118127
import de.bytefish.jsqlserverbulkinsert.test.model.Person;
119128
import de.bytefish.jsqlserverbulkinsert.SqlServerBulkInsert;
120129
import de.bytefish.jsqlserverbulkinsert.test.base.TransactionalTestBase;
130+
import de.bytefish.jsqlserverbulkinsert.test.utils.MeasurementUtils;
121131
import org.junit.Assert;
122132
import org.junit.Test;
123133

@@ -145,8 +155,11 @@ public class IntegrationTest extends TransactionalTestBase {
145155
PersonMapping mapping = new PersonMapping();
146156
// Create the Bulk Inserter:
147157
SqlServerBulkInsert<Person> bulkInsert = new SqlServerBulkInsert<>(mapping);
148-
// Now save all entities of a given stream:
149-
bulkInsert.saveAll(connection, persons.stream());
158+
// Measure the Bulk Insert time:
159+
MeasurementUtils.MeasureElapsedTime("Bulk Insert 1000000 Entities", () -> {
160+
// Now save all entities of a given stream:
161+
bulkInsert.saveAll(connection, persons.stream());
162+
});
150163
// And assert all have been written to the database:
151164
Assert.assertEquals(numEntities, getRowCount());
152165
}

0 commit comments

Comments
 (0)