how to use phoenix transaction with jdbc client - jdbc

I am using phoenix with transactions, as from phoenix.apache.org/transactions.html, I need to set phoenix.transactions.enabled=true from my client-side.
But how to set that in a java jdbc client?

I got it.
Properties info = new Properties();
info.setProperty("phoenix.transactions.enabled", Boolean.TRUE.toString());
con = DriverManager.getConnection(url, info);

Related

Spring ActiveMQ use JDBC with tcp

Given the fact that the only requirement I have is to use an instance of ActiveMQ, how would I make my ActiveMQ to use a JDBC connection without creating a embedded one with VM transport.
This is my factory bean:
#Bean
public ActiveMQConnectionFactory connectionFactory() {
logger.info("ActiveMQConnectionFactory");
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(brokerUrl);
activeMQConnectionFactory.setTrustAllPackages(true);
RedeliveryPolicy redeliveryPolicy = new RedeliveryPolicy();
redeliveryPolicy.setRedeliveryDelay(15000);
redeliveryPolicy.setMaximumRedeliveries(-1);
activeMQConnectionFactory.setRedeliveryPolicy(redeliveryPolicy);
return activeMQConnectionFactory;
}
I have an image of activemq exposed in the URL: tcp//0.0.0.0:61616 but even with the JDBC adapter configured as shown below I'm not able to persist messages in the SQL server, activemq is ingoring this and use the KahaDB as default. The only way found to use the jdbc is to change from tcp to vm:localhost but this creates an embedded activemq.
#Bean
public BrokerService broker(DataSource dataSource, ActiveMQConnectionFactory activeMQConnectionFactory) throws Exception {
logger.info("BrokerService");
final BrokerService broker = new BrokerService();
JDBCPersistenceAdapter jdbc = new JDBCPersistenceAdapter(dataSource, new OpenWireFormat());
jdbc.setUseLock(true);
Statements statements = jdbc.getStatements();
statements.setBinaryDataType(BINARY_DATA_TYPE);
broker.setUseJmx(true);
broker.setPersistent(true);
broker.setPersistenceAdapter(jdbc);
broker.addConnector(format("vm:(broker:(tcp://localhost:61616,network:static:%s)?persistent=true)", brokerUrl));
logger.info("BrokerService URL: " + broker.getTransportConnectors().get(0).getConnectUri().toString());
return broker;
}
Recommend using xml file to configure the broker. Easier to manage vs coding up a broker.
ActiveMQ JDBC info:
ref: https://activemq.apache.org/jdbc-support
Persistence Adapter info:
ref: https://activemq.apache.org/persistence
Starting an embedded broker referencing an xml file:
ref: https://activemq.apache.org/vm-transport-reference
specifically:
vm://localhost?brokerConfig=xbean:activemq.xml

How to set configuration using application.properties in Hibernate?

I am new to Spring boot development.
What I did?
I tested the sample MySQL integration with Spring boot and tested it works fine.
What I am trying?
I am trying to use Hibernate APIs with my existing project. So I create a util class for creating Hibernate sessionFactory.
My code:
public static void initSessionFactory() {
System.out.print("initSessionFactory");
if (sessionFactory == null) {
try {
Configuration configuration = new Configuration();
//***** I don't want configuration here. ******
// Hibernate settings equivalent to hibernate.cfg.xml's properties
//Properties settings = new Properties();
// settings.put(Environment.DRIVER, "com.mysql.cj.jdbc.Driver");
// settings.put(Environment.URL, "jdbc:mysql://localhost:3306/hibernate_db?useSSL=false");
// settings.put(Environment.USER, "root");
// settings.put(Environment.PASS, "root");
// settings.put(Environment.DIALECT, "org.hibernate.dialect.MySQL5Dialect");
// settings.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
// settings.put(Environment.HBM2DDL_AUTO, "create-drop");
// settings.put(Environment.SHOW_SQL, "true");
// configuration.setProperties(settings);
//configuration.addAnnotatedClass(Student.class);
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
session = sessionFactory.openSession();
} catch (Exception e) {
e.printStackTrace();
}
}
}
My application.properties:(It works fine for without Hibernate)
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/tcc
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
spring.jpa.show-sql=true
My problem:
I already configured the jdbc environment & other configuration in my application.properties. It works fine. So I don't want to repeat the same configuration in my Java code. So I commented this configuration.
But without Java configuration it throws "The application must supply JDBC connections" error.
My Question:
How to set configuration from application.properties for Hibernate?
When you set the properties below in your app.properties, the Spring Boot will already make your DB connection ready when the application start.
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword
spring.datasource.driver-class-name =com.mysql.jdbc.Driver
You do not need to create SessionFactory , ServiceRegistery etc. even if you do not want to manage DB connection manually.
This link also explain the steps in more detail. You will see that there is no any custom bean, factory or registerer to establish DB connection. Please see it.
https://spring.io/guides/gs/accessing-data-mysql/

Spring Boot ActiveMQJMSConnectionFactory to connect to a JBoss EAP 6.1 HornetQ

I'm not sure if what I'm trying to do is possible but basically I'm trying to update the current implementation from HornetQ to ActiveMQ making use of Artemis.
My system is a JMS consumer from the HornetQ.
The current implementation works if I use "HornetQJMSConnectionFactory" but when I change to ActiveMQJMSConnectionFactory it is not able to connect.
In order to test the new implementation, I've spun up a local instance of ActiveMQ and works with the new implementation.
So I've tried multiple different things including forcing protocol =HORNETQ and nothing works.
No compilation error, "only":
ERROR o.s.j.l.DefaultMessageListenerContainer.refreshConnectionUntilSuccessful - Could not refresh JMS Connection for destination 'QueueX' - retrying using FixedBackOff{interval=5000, currentAttempts=1, maxAttempts=unlimited}. Cause: Failed to create session factory; nested exception is ActiveMQConnectionTimedOutException[errorType=CONNECTION_TIMEDOUT message=AMQ219013: Timed out waiting to receive cluster topology. Group:null]
Old Implementation
private ConnectionFactory createConnectionFactory(SyncProperties.SmpJmsServer jmsServer) {
final String className = "org.hornetq.core.remoting.impl.netty.NettyConnectorFactory";
Map<String, Object> params = new HashMap<String, Object>();
params.put("host", getJmsHost());
params.put("port", getJmsPort());
TransportConfiguration transportConfiguration = new TransportConfiguration(className, params);
HornetQJMSConnectionFactory hornetQJMSConnectionFactory = new HornetQJMSConnectionFactory(false, transportConfiguration);
hornetQJMSConnectionFactory.setConnectionTTL(300000);
hornetQJMSConnectionFactory.setConsumerWindowSize(0);
UserCredentialsConnectionFactoryAdapter adapter = new UserCredentialsConnectionFactoryAdapter();
adapter.setTargetConnectionFactory(hornetQJMSConnectionFactory);
adapter.setUsername(getJmsUsername());
adapter.setPassword(getJmsPassword());
CachingConnectionFactory smpCachingConnectionFactory = new CachingConnectionFactory(adapter);
return smpCachingConnectionFactory;
}
New Implementation
public ConnectionFactory createActiveMQJMSConnectionFactory() {
ActiveMQJMSConnectionFactory activeMQJMSConnectionFactory = new ActiveMQJMSConnectionFactory(false, amqTransportConfiguration());
activeMQJMSConnectionFactory.setConnectionTTL(300000);
activeMQJMSConnectionFactory.setConsumerWindowSize(0);
UserCredentialsConnectionFactoryAdapter adapter = new UserCredentialsConnectionFactoryAdapter();
adapter.setTargetConnectionFactory(activeMQJMSConnectionFactory);
adapter.setUsername(getJmsUsername());
adapter.setPassword(getJmsPassword());
CachingConnectionFactory smpCachingConnectionFactory = new CachingConnectionFactory(adapter);
return smpCachingConnectionFactory;
}
#Bean("amqTransportConfiguration")
public TransportConfiguration amqTransportConfiguration() {
return new TransportConfiguration("org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnectorFactory", getParams());
}
static Map<String, Object> getParams() {
Map<String, Object> params = new HashMap<String, Object>();
params.put("host", getJmsHost());
params.put("port", getJmsPort());
return params;
}
Thanks for the help.
Although ActiveMQ Artemis is based on the HornetQ code-base an ActiveMQ Artemis client won't be able to talk with a HornetQ broker. Each client sends a protocol/client identifier when it connects. This ID is different between ActiveMQ Artemis clients and HornetQ clients. A HornetQ broker will not recognize the ID sent by the ActiveMQ Artemis client and therefore will not complete a handshake.
That said, we have worked to ensure that HornetQ clients can still talk to an ActiveMQ Artemis broker. That's what the org.apache.activemq.artemis.core.protocol.hornetq.HornetQProtocolManager provides.
In any case, upgrading your client won't really do much for you anyway. If you want to upgrade anything I recommend you upgrade EAP or even move to a standalone version of ActiveMQ Artemis so you can get the latest fixes & features.

Using JTA in simple JDBC application

I have read on-line about JTA. Now, I want to implement it in a simple possible program.
I have the code below of JDBC, in which I am managing the transaction explicitly (by JDBC API's).
Code snippet:
String ssnID = null;
ssnID = "9";
String sql;
String sql_delete = null;
sql_delete = "Delete from USER where ssnId = " + "9";
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL,USER,PASS);
stmt = conn.createStatement();
conn.setAutoCommit(false); <-------- (1)
stmt.executeUpdate(sql_delete);
//other code as part of transaction
conn.commit(); <---------(2)
In above I am using JDBC API's for manual transaction management.
My question is, how can I use JTA for the transaction management? Is JTA transaction management different from what JDBC uses?
Any explanation to understand this would be of great help.

TIBCO EMS set queue exclusive property programmatically

How can I set the exclusive/non-exclusive property of a tibco queue programmatically? I want to be able to set the queue as non-exclusive when I crate it in my application.
For example, if I use the following code to craete the queue:
QueueConnectionFactory factory = new TIBCO.EMS.QueueConnectionFactory(serverUrl);
QueueConnection connection = factory.CreateQueueConnection(userName, password);
QueueSession session = connection.CreateQueueSession(false, Session.AUTO_ACKNOWLEDGE);
TIBCO.EMS.Queue queue = session.CreateQueue(queueName);
How can I set the queue's properties?
You will need the TibjmsAdmin API to do that. The JavaDoc of the API can be found here
Then try this:
TibjmsAdmin jmsAdmin = new TibjmsAdmin("tcp://localhost:7222", "admin", "admin");
QueueInfo qi = jmsAdmin.getQueue("my.queue");
qi.setExclusive(true);
HTH,
Hendrik

Resources