/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.openejb.resource.jdbc;
import org.apache.openejb.jee.EjbJar;
import org.apache.openejb.jee.SingletonBean;
import org.apache.openejb.junit.ApplicationComposer;
import org.apache.openejb.testing.Configuration;
import org.apache.openejb.testing.Module;
import org.apache.openejb.resource.jdbc.managed.local.ManagedConnection;
import org.junit.After;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.EJBContext;
import javax.ejb.LocalBean;
import javax.ejb.Singleton;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.sql.DataSource;
import javax.transaction.Transaction;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;
import java.util.Properties;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@RunWith(ApplicationComposer.class)
public class ManagedDataSourceTest {
private static final String URL =
"jdbc:hsqldb:mem:managed;hsqldb.tx=MVCC"; // mvcc otherwise multiple
transaction tests will fail
private static final String USER = "sa";
private static final String PASSWORD = "";
private static final String TABLE = "PUBLIC.MANAGED_DATASOURCE_TEST";
@EJB
private Persister persistManager;
@Resource
private DataSource ds;
@BeforeClass
public static void createTable() throws SQLException,
ClassNotFoundException {
Class.forName("org.hsqldb.jdbcDriver");
final Connection connection = DriverManager.getConnection(URL,
USER, PASSWORD);
final Statement statement = connection.createStatement();
statement.execute("CREATE TABLE " + TABLE + "(ID INTEGER)");
statement.close();
connection.commit();
connection.close();
}
@Configuration
public Properties config() {
final Properties p = new Properties();
p.put("openejb.jdbc.datasource-creator", "dbcp-alternative");
p.put("managed", "new://Resource?type=DataSource");
p.put("managed.JdbcDriver", "org.hsqldb.jdbcDriver");
p.put("managed.JdbcUrl", URL);
p.put("managed.UserName", USER);
p.put("managed.Password", PASSWORD);
p.put("managed.JtaManaged", "true");
return p;
}
@Module
public EjbJar app() throws Exception {
return new EjbJar()
.enterpriseBean(new
SingletonBean(Persister.class).localBean())
.enterpriseBean(new
SingletonBean(OtherPersister.class).localBean());
}
@LocalBean
@Singleton
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public static class OtherPersister {
@Resource(name = "managed")
private DataSource ds;
@Resource
private EJBContext context;
public void save() throws SQLException {
ManagedDataSourceTest.save(ds, 10);
}
public void saveAndRollback() throws SQLException {
ManagedDataSourceTest.save(ds, 11);
context.setRollbackOnly();
}
}
@LocalBean
@Singleton
public static class Persister {
@Resource(name = "managed")
private DataSource ds;
@Resource
private EJBContext context;
@EJB
private OtherPersister other;
public void save() throws SQLException {
ManagedDataSourceTest.save(ds, 1);
}
public void saveAndRollback() throws SQLException {
ManagedDataSourceTest.save(ds, 2);
context.setRollbackOnly();
}
public void saveTwice() throws SQLException {
ManagedDataSourceTest.save(ds, 3);
ManagedDataSourceTest.save(ds, 4);
}
public void rollbackMultipleSave() throws SQLException {
ManagedDataSourceTest.save(ds, 5);
ManagedDataSourceTest.save(ds, 6);
context.setRollbackOnly();
}
public void saveInThisTxAndAnotherOne() throws SQLException {
ManagedDataSourceTest.save(ds, 7);
other.save();
}
public void saveInThisTxAndRollbackInAnotherOne() throws
SQLException {
ManagedDataSourceTest.save(ds, 8);
other.saveAndRollback();
}
}
@Test
public void commit() throws SQLException {
persistManager.save();
assertTrue(exists(1));
}
@Test
public void rollback() throws SQLException {
persistManager.saveAndRollback();
assertFalse(exists(2));
}
@Test
public void commit2() throws SQLException {
persistManager.saveTwice();
assertTrue(exists(3));
assertTrue(exists(4));
}
@Test
public void rollback2() throws SQLException {
persistManager.rollbackMultipleSave();
assertFalse(exists(5));
assertFalse(exists(6));
}
@Test
public void saveDifferentTx() throws SQLException {
persistManager.saveInThisTxAndAnotherOne();
assertTrue(exists(7));
assertTrue(exists(10));
}
@Test
public void saveRollbackDifferentTx() throws SQLException {
persistManager.saveInThisTxAndRollbackInAnotherOne();
assertTrue(exists(8));
assertFalse(exists(12));
}
@After
public void checkTxMapIsEmpty() throws Exception { // avoid memory leak
final Field map =
ManagedConnection.class.getDeclaredField("CONNECTION_BY_TX_BY_DS");
map.setAccessible(true);
final Map<DataSource, Map<Transaction, Connection>> instance =
(Map<DataSource, Map<Transaction, Connection>> ) map.get(null);
assertEquals(1, instance.size());
assertEquals(0, instance.values().iterator().next().size());
}
private static boolean exists(int id) throws SQLException {
final Connection connection = DriverManager.getConnection(URL,
USER, PASSWORD);
final Statement statement = connection.createStatement();
final ResultSet result = statement.executeQuery("SELECT count(*) AS
NB FROM " + TABLE + " WHERE ID = " + id);
try {
assertTrue(result.next());
return result.getInt(1) == 1;
} finally {
statement.close();
connection.close();
}
}
private static void save(final DataSource ds, int id) throws
SQLException {
execute(ds, "INSERT INTO " + TABLE + "(ID) VALUES(" + id + ")");
}
private static void execute(final DataSource ds, final String sql)
throws SQLException {
final Connection connection = ds.getConnection();
final Statement statement = connection.createStatement();
statement.executeUpdate(sql);
statement.close();
connection.close();
}
}
Le 3 sept. 2013 12:01, "tschuler" <[email protected]> a écrit :
> Hi Romain!
>
> Can you directly post the ManagedDataSourceTest.java file because I got
> connection timeouts accessing the apache tomee svn repository.
>
> Best regards,
> Thomas
>
> Von: Romain Manni-Bucau [via OpenEJB] [mailto:
> [email protected]]
> Gesendet: Dienstag, 3. September 2013 09:50
> An: Thomas Schuler
> Betreff: Re: AW: Fired cron trigger not available
>
> Hi
>
> This test defines a datasource called 'managed'
>
> http://svn.apache.org/repos/asf/tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/resource/jdbc/ManagedDataSourceTest.java
>
> Just add in src/test/resources a file import-managed.sql with a statement
> by line created from hsql scripts of quartz
> Le 3 sept. 2013 09:33, "tschuler" <[hidden
> email]</user/SendEmail.jtp?type=node&node=4664964&i=0>> a écrit :
>
> > Hi Romain!
> >
> > As I am neither familiar with the application composer and the in memory
> > database:
> > Can you provide me a running example how to initialize and use the in
> > memory
> > database for tests?
> >
> > Best regards,
> > Thomas
> >
> >
> >
> > --
> > View this message in context:
> >
> http://openejb.979440.n4.nabble.com/Fired-cron-trigger-not-available-tp4664936p4664963.html
> > Sent from the OpenEJB User mailing list archive at Nabble.com.
> >
>
> ________________________________
> If you reply to this email, your message will be added to the discussion
> below:
>
> http://openejb.979440.n4.nabble.com/Fired-cron-trigger-not-available-tp4664936p4664964.html
> To unsubscribe from Fired cron trigger not available, click here<
> http://openejb.979440.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4664936&code=dGhvbWFzLnNjaHVsZXJAb3BlbnRleHQuY29tfDQ2NjQ5MzZ8LTE4NTIyNTQ0OTI=
> >.
> NAML<
> http://openejb.979440.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml
> >
>
>
>
>
> --
> View this message in context:
> http://openejb.979440.n4.nabble.com/Fired-cron-trigger-not-available-tp4664936p4664969.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.