Search in sources :

Example 6 with XADataSource

use of javax.sql.XADataSource in project spring-boot by spring-projects.

the class XADataSourceAutoConfigurationTests method wrapExistingXaDataSource.

@Test
public void wrapExistingXaDataSource() throws Exception {
    ApplicationContext context = createContext(WrapExisting.class);
    context.getBean(DataSource.class);
    XADataSource source = context.getBean(XADataSource.class);
    MockXADataSourceWrapper wrapper = context.getBean(MockXADataSourceWrapper.class);
    assertThat(wrapper.getXaDataSource()).isEqualTo(source);
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) XADataSource(javax.sql.XADataSource) JDBCXADataSource(org.hsqldb.jdbc.pool.JDBCXADataSource) Test(org.junit.Test)

Example 7 with XADataSource

use of javax.sql.XADataSource in project spring-boot by spring-projects.

the class XADataSourceAutoConfiguration method createXaDataSource.

private XADataSource createXaDataSource() {
    String className = this.properties.getXa().getDataSourceClassName();
    if (!StringUtils.hasLength(className)) {
        className = DatabaseDriver.fromJdbcUrl(this.properties.determineUrl()).getXaDataSourceClassName();
    }
    Assert.state(StringUtils.hasLength(className), "No XA DataSource class name specified");
    XADataSource dataSource = createXaDataSourceInstance(className);
    bindXaProperties(dataSource, this.properties);
    return dataSource;
}
Also used : XADataSource(javax.sql.XADataSource)

Example 8 with XADataSource

use of javax.sql.XADataSource in project spring-boot by spring-projects.

the class BitronixXADataSourceWrapperTests method wrap.

@Test
public void wrap() throws Exception {
    XADataSource dataSource = mock(XADataSource.class);
    BitronixXADataSourceWrapper wrapper = new BitronixXADataSourceWrapper();
    DataSource wrapped = wrapper.wrapDataSource(dataSource);
    assertThat(wrapped).isInstanceOf(PoolingDataSourceBean.class);
    assertThat(((PoolingDataSourceBean) wrapped).getDataSource()).isSameAs(dataSource);
}
Also used : XADataSource(javax.sql.XADataSource) DataSource(javax.sql.DataSource) XADataSource(javax.sql.XADataSource) Test(org.junit.Test)

Example 9 with XADataSource

use of javax.sql.XADataSource in project spring-boot by spring-projects.

the class PoolingDataSourceBeanTests method setDataSource.

@Test
public void setDataSource() throws Exception {
    XADataSource dataSource = mock(XADataSource.class);
    XAConnection xaConnection = mock(XAConnection.class);
    Connection connection = mock(Connection.class);
    given(dataSource.getXAConnection()).willReturn(xaConnection);
    given(xaConnection.getConnection()).willReturn(connection);
    this.bean.setDataSource(dataSource);
    this.bean.setBeanName("beanName");
    this.bean.afterPropertiesSet();
    this.bean.init();
    this.bean.createPooledConnection(dataSource, this.bean);
    verify(dataSource).getXAConnection();
    TransactionManagerServices.getTaskScheduler().shutdown();
}
Also used : XADataSource(javax.sql.XADataSource) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) XAConnection(javax.sql.XAConnection) Test(org.junit.Test)

Example 10 with XADataSource

use of javax.sql.XADataSource in project aries by apache.

the class TxDBServlet method doGet.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    pw = response.getWriter();
    pw.write(LOGO_HEADER);
    // Get the bundle context from ServletContext attributes
    BundleContext ctx = (BundleContext) getServletContext().getAttribute(OSGI_BUNDLECONTEXT_ATTRIBUTE);
    pw.println("<html>");
    pw.println("<head>");
    pw.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"tableTemplate.css\"/>");
    pw.println("<body>");
    pw.println("<h2 align=center><p><font face=\"Tahoma\">Sample Application for JDBC usage in JTA Transactions.</font></h2>");
    pw.println("<p><p>");
    ServiceReference tmServiceRef = ctx.getServiceReference("javax.transaction.TransactionManager");
    ServiceReference derbyServiceRef = ctx.getServiceReference(DataSourceFactory.class.getName());
    if (tmServiceRef == null || derbyServiceRef == null) {
        pw.println("<font face=\"Tahoma\">TransactionManager or Derby driver are not available in the OSGI registry.</font><br>");
    } else {
        TransactionManager tm = (TransactionManager) ctx.getService(tmServiceRef);
        DataSourceFactory derbyRegistry = (DataSourceFactory) ctx.getService(derbyServiceRef);
        try {
            // Set the needed properties for the database connection
            Properties props = new Properties();
            props.put(DataSourceFactory.JDBC_URL, "jdbc:derby:txDemo");
            props.put(DataSourceFactory.JDBC_DATABASE_NAME, "txDemo");
            props.put(DataSourceFactory.JDBC_USER, "");
            props.put(DataSourceFactory.JDBC_PASSWORD, "");
            XADataSource xaDataSource = derbyRegistry.createXADataSource(props);
            pw.println("<center><form><table><tr><td align='right'>Value: </td><td align=left><input type='text' name='value' value='' size='12'/><input type='submit' name='action' value='InsertAndCommit' size='100'/></td></tr>");
            pw.println("<tr><td align='right'>Value: </td><td align=left><input type='text' name='value' value='' size='12'/><input type='submit' name='action' value='InsertAndRollback' size='100'/></center></td></tr>");
            pw.println("<tr colspan='2' align='center'><td>&nbsp;</td><td><input type='submit' name='action' value='cleanTable' size='100' />&nbsp;<input type='submit' name='action' value='printTable' size='100'/></td><tr></table></form></center>");
            String value = request.getParameter("value");
            String action = request.getParameter("action");
            if (action != null && action.equals("InsertAndCommit")) {
                insertIntoTransaction(xaDataSource, tm, value, true);
            } else if (action != null && action.equals("InsertAndRollback")) {
                insertIntoTransaction(xaDataSource, tm, value, false);
            } else if (action != null && action.equals("cleanTable")) {
                cleanTable(xaDataSource);
            }
            printTable(xaDataSource);
        } catch (Exception e) {
            pw.println("<font face=\"Tahoma\">Unexpected exception occurred " + e.toString() + ".</font><br>");
            e.printStackTrace(pw);
        }
    }
    pw.println("</body>");
    pw.println("</html>");
    pw.flush();
}
Also used : XADataSource(javax.sql.XADataSource) DataSourceFactory(org.osgi.service.jdbc.DataSourceFactory) TransactionManager(javax.transaction.TransactionManager) Properties(java.util.Properties) ServletException(javax.servlet.ServletException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) IOException(java.io.IOException) NotSupportedException(javax.transaction.NotSupportedException) SQLException(java.sql.SQLException) SystemException(javax.transaction.SystemException) RollbackException(javax.transaction.RollbackException) HeuristicMixedException(javax.transaction.HeuristicMixedException) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

XADataSource (javax.sql.XADataSource)14 DataSource (javax.sql.DataSource)8 CommonDataSource (javax.sql.CommonDataSource)5 Test (org.junit.Test)4 TransactionManager (javax.transaction.TransactionManager)3 ManagedXADataSource (org.apache.openejb.resource.jdbc.managed.xa.ManagedXADataSource)3 DriverDataSource (org.apache.aries.tx.control.jdbc.common.impl.DriverDataSource)2 ManagedDataSource (org.apache.openejb.resource.jdbc.managed.local.ManagedDataSource)2 BundleContext (org.osgi.framework.BundleContext)2 BoneCPDataSource (com.jolbox.bonecp.BoneCPDataSource)1 IOException (java.io.IOException)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 Hashtable (java.util.Hashtable)1 Properties (java.util.Properties)1 ServletException (javax.servlet.ServletException)1 XAConnection (javax.sql.XAConnection)1 HeuristicMixedException (javax.transaction.HeuristicMixedException)1 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)1 NotSupportedException (javax.transaction.NotSupportedException)1