Skip to content

Commit

Permalink
identity support fix for h2 and derby
Browse files Browse the repository at this point in the history
  • Loading branch information
delchev committed Nov 27, 2017
1 parent a0fda30 commit 89ed17e
Show file tree
Hide file tree
Showing 16 changed files with 468 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,14 @@ private void forceRelaseConnection() throws SQLException {
*/
protected WrappedConnection getOldestConnection() {
WrappedConnection oldestConnection = null;
for (WrappedConnection connection : connections) {
if (oldestConnection == null) {
oldestConnection = connection;
}
if (oldestConnection.getTimeAcquired() < connection.getTimeAcquired()) {
oldestConnection = connection;
synchronized (connections) {
for (WrappedConnection connection : connections) {
if (oldestConnection == null) {
oldestConnection = connection;
}
if (oldestConnection.getTimeAcquired() < connection.getTimeAcquired()) {
oldestConnection = connection;
}
}
}
return oldestConnection;
Expand Down
18 changes: 18 additions & 0 deletions modules/database-persistence/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@
<version>10.12.1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.193</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public Object insert(Connection connection, PersistenceTableModel tableModel, Ob
}

private Object getLastInserted(Connection connection, PersistenceTableModel tableModel, Object pojo) throws SQLException {
LastValueIdentityBuilder identityBuilder = SqlFactory.getNative(SqlFactory.deriveDialect(connection)).lastval();
LastValueIdentityBuilder identityBuilder = SqlFactory.getNative(SqlFactory.deriveDialect(connection)).lastval(tableModel.getTableName());
String sql = identityBuilder.build();
PreparedStatement preparedStatement = null;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.derby.jdbc.EmbeddedDataSource;
import org.junit.Before;

Expand All @@ -35,7 +38,6 @@ public class AbstractPersistenceManagerTest {
@Before
public void setUp() {
try {

this.dataSource = createDataSource("target/tests/derby");
} catch (Exception e) {
e.printStackTrace();
Expand Down Expand Up @@ -63,11 +65,36 @@ public DataSource getDataSource() {
*/
protected DataSource createDataSource(String name) throws Exception {
try {
DataSource dataSource = new EmbeddedDataSource();
String derbyRoot = prepareRootFolder(name);
((EmbeddedDataSource) dataSource).setDatabaseName(derbyRoot);
((EmbeddedDataSource) dataSource).setCreateDatabase("create");
return dataSource;
Properties databaseProperties = new Properties();
InputStream in = AbstractPersistenceManagerTest.class.getResourceAsStream("/database.properties");
if (in != null) {
databaseProperties.load(in);
}
String database = System.getProperty("database");
if (database == null) {
database = "derby";
}

if ("derby".equals(database)) {
DataSource embeddedDataSource = new EmbeddedDataSource();
String derbyRoot = prepareRootFolder(name);
((EmbeddedDataSource) embeddedDataSource).setDatabaseName(derbyRoot);
((EmbeddedDataSource) embeddedDataSource).setCreateDatabase("create");
return embeddedDataSource;
}
BasicDataSource basicDataSource = new BasicDataSource();
String databaseDriver = databaseProperties.getProperty(database + ".driver");
basicDataSource.setDriverClassName(databaseDriver);
String databaseUrl = databaseProperties.getProperty(database + ".url");
basicDataSource.setUrl(databaseUrl);
String databaseUsername = databaseProperties.getProperty(database + ".udername");
basicDataSource.setUsername(databaseUsername);
String databasePassword = databaseProperties.getProperty(database + ".password");
basicDataSource.setPassword(databasePassword);
basicDataSource.setDefaultAutoCommit(true);

return basicDataSource;

} catch (IOException e) {
throw new Exception(e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2017 SAP and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
* Contributors:
* SAP - initial API and implementation
*/

package org.eclipse.dirigible.database.persistence.test;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

/**
* The Class Offer.
*/
@Table(name = "OFFERS")
public class Offer {

/** The id. */
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "OFFER_ID", columnDefinition = "BIGINT", nullable = false)
private long id;

/** The subject. */
@Column(name = "OFFER_SUBJECT", columnDefinition = "VARCHAR", nullable = false, length = 512)
private String subject;

/**
* Gets the id.
*
* @return the id
*/
public long getId() {
return id;
}

/**
* Sets the id.
*
* @param id the new id
*/
public void setId(long id) {
this.id = id;
}

/**
* Gets the subject.
*
* @return the subject
*/
public String getSubject() {
return subject;
}

/**
* Sets the subject.
*
* @param subject the new subject
*/
public void setSubject(String subject) {
this.subject = subject;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
/*
* Copyright (c) 2017 SAP and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
* Contributors:
* SAP - initial API and implementation
*/

package org.eclipse.dirigible.database.persistence.test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import org.eclipse.dirigible.database.persistence.PersistenceManager;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* The Persistence Manager Generated Value Table Test.
*/
public class PersistenceManagerGeneratedValueIdentityTest extends AbstractPersistenceManagerTest {

private static final Logger logger = LoggerFactory.getLogger(PersistenceManagerGeneratedValueIdentityTest.class);

/**
* Ordered CRUD tests.
*
* @throws SQLException
* the SQL exception
*/
@Test
public void orderedCrudTests() throws SQLException {
String database = System.getProperty("database");
if (database == null) {
database = "derby";
}
if (!"sybase".equals(database) && !"h2".equals(database) && !"derby".equals(database)) {
logger.warn("Skipped IDENTITY test for database: " + database);
return;
}

PersistenceManager<Offer> persistenceManager = new PersistenceManager<Offer>();
Connection connection = null;
try {
connection = getDataSource().getConnection();
// create table
createTableForPojo(connection, persistenceManager);
// check whether it is created successfully
assertTrue(existsTable(connection, persistenceManager));
// insert a record in the table for a pojo
insertPojo(connection, persistenceManager);
// insert a record in the table for a pojo
insertSecondPojo(connection, persistenceManager);
// insert a record in the table for a pojo
insertThirdPojo(connection, persistenceManager);
// get the list of all the records
findAllPojo(connection, persistenceManager);
// drop the table
dropTableForPojo(connection, persistenceManager);
} finally {
if (connection != null) {
connection.close();
}
}
}

/**
* Creates the table for pojo.
*
* @param connection
* the connection
* @param persistenceManager
* the persistence manager
* @throws SQLException
* the SQL exception
*/
public void createTableForPojo(Connection connection, PersistenceManager<Offer> persistenceManager) throws SQLException {
persistenceManager.tableCreate(connection, Offer.class);
}

/**
* Exists table.
*
* @param connection
* the connection
* @param persistenceManager
* the persistence manager
* @return true, if successful
* @throws SQLException
* the SQL exception
*/
public boolean existsTable(Connection connection, PersistenceManager<Offer> persistenceManager) throws SQLException {
return persistenceManager.tableExists(connection, Offer.class);
}

/**
* Insert pojo.
*
* @param connection
* the connection
* @param persistenceManager
* the persistence manager
* @throws SQLException
* the SQL exception
*/
public void insertPojo(Connection connection, PersistenceManager<Offer> persistenceManager) throws SQLException {
Offer offer = new Offer();
offer.setSubject("Subject 1");
persistenceManager.insert(connection, offer);
assertNotEquals(0, offer.getId());
}

/**
* Insert second pojo.
*
* @param connection
* the connection
* @param persistenceManager
* the persistence manager
* @throws SQLException
* the SQL exception
*/
public void insertSecondPojo(Connection connection, PersistenceManager<Offer> persistenceManager) throws SQLException {
Offer offer = new Offer();
offer.setSubject("Subject 2");
persistenceManager.insert(connection, offer);
}

/**
* Insert third pojo.
*
* @param connection
* the connection
* @param persistenceManager
* the persistence manager
* @throws SQLException
* the SQL exception
*/
public void insertThirdPojo(Connection connection, PersistenceManager<Offer> persistenceManager) throws SQLException {
Offer offer = new Offer();
offer.setSubject("Subject 3");
persistenceManager.insert(connection, offer);
}

/**
* Find all pojo.
*
* @param connection
* the connection
* @param persistenceManager
* the persistence manager
* @throws SQLException
* the SQL exception
*/
public void findAllPojo(Connection connection, PersistenceManager<Offer> persistenceManager) throws SQLException {
List<Offer> list = persistenceManager.findAll(connection, Offer.class);

assertNotNull(list);
assertFalse(list.isEmpty());
assertEquals(3, list.size());
Offer offer = list.get(0);
assertEquals("Subject 1", offer.getSubject());

System.out.println(offer.getId());

}

/**
* Drop table for pojo.
*
* @param connection
* the connection
* @param persistenceManager
* the persistence manager
* @throws SQLException
* the SQL exception
*/
public void dropTableForPojo(Connection connection, PersistenceManager<Offer> persistenceManager) throws SQLException {
persistenceManager.tableDrop(connection, Offer.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
postgres.driver=org.postgresql.Driver
postgres.url=jdbc:postgresql://<host>:<port>/<database>
postgres.username=postgres
postgres.password=
sybase.driver=com.sybase.jdbc4.jdbc.SybDriver
sybase.url=jdbc:sybase:Tds:<host>:<port>
sybase.username=
sybase.password=
h2.driver=org.h2.Driver
h2.url=jdbc:h2:mem:dirigible;DB_CLOSE_DELAY=-1
h2.username=sa
h2.password=
Loading

0 comments on commit 89ed17e

Please sign in to comment.