nevado-jms/src/test/java/org/skyscreamer/nevado/jms/facilities/ConnectionCloseTest.java
/**
     * Test that a connection rolls back a transaction when it is closed.
     *
     * @throws JMSException
     */
Test that a connection rolls back a transaction when it is closed.
Test that a connection rolls back a transaction when it is closed .
@Test
    public void testRollbackTransaction() throws JMSException
    {
        NevadoQueue testQueue = new NevadoQueue("testQueue" + RandomData.readInt());
        Connection testConnection = createConnection(getConnectionFactory());
        testConnection.start();
        Session controlSession = createSession();
        TextMessage msg1 = controlSession.createTextMessage(RandomData.readString());
        controlSession.createProducer(testQueue).send(msg1);
        Session session = testConnection.createSession(true, Session.SESSION_TRANSACTED);
        TextMessage msgOut = (TextMessage)session.createConsumer(testQueue).receive(1000);
        Assert.assertEquals(msg1.getText(), msgOut.getText());
        TextMessage msg2 = session.createTextMessage(RandomData.readString());
        session.createProducer(testQueue).send(msg2);
        testConnection.close();
        Session testSession = createSession();
        MessageConsumer consumer = testSession.createConsumer(testQueue);
        TextMessage afterTxMsg = (TextMessage)consumer.receive(1000);
        Assert.assertNotNull(afterTxMsg);
        Assert.assertEquals(msg1.getText(), afterTxMsg.getText());
        Assert.assertNull(consumer.receive(100));
        deleteQueue(testQueue);
    }
@ Test public void testRollbackTransaction ( ) throws JMSException { NevadoQueue testQueue = new NevadoQueue ( "testQueue" + RandomData . readInt ( ) ) ; Connection testConnection = createConnection ( getConnectionFactory ( ) ) ; testConnection . start ( ) ; Session controlSession = createSession ( ) ; TextMessage msg1 = controlSession . createTextMessage ( RandomData . readString ( ) ) ; controlSession . createProducer ( testQueue ) . send ( msg1 ) ; Session session = testConnection . createSession ( true , Session . SESSION_TRANSACTED ) ; TextMessage msgOut = ( TextMessage ) session . createConsumer ( testQueue ) . receive ( 1000 ) ; Assert . assertEquals ( msg1 . getText ( ) , msgOut . getText ( ) ) ; TextMessage msg2 = session . createTextMessage ( RandomData . readString ( ) ) ; session . createProducer ( testQueue ) . send ( msg2 ) ; testConnection . close ( ) ; Session testSession = createSession ( ) ; MessageConsumer consumer = testSession . createConsumer ( testQueue ) ; TextMessage afterTxMsg = ( TextMessage ) consumer . receive ( 1000 ) ; Assert . assertNotNull ( afterTxMsg ) ; Assert . assertEquals ( msg1 . getText ( ) , afterTxMsg . getText ( ) ) ; Assert . assertNull ( consumer . receive ( 100 ) ) ; deleteQueue ( testQueue ) ; }
Test that a connection rolls back a transaction when it is closed.
Test that a connection rolls back a transaction when it is closed .
// A temporary queue (but not a TemporaryQueue!) is needed here since we're testing across connections // Do some stuff, roll it back // Confirm the rollback worked // Clean up