My data access objects takes a DataSource as a parameter which works perfectly if they are deployed within an app server.
I'm wondering how I can assemble a data source from within a simple Java class. This might also be useful for unit testing?!
There's nothing magic about a DataSource. It's just an interface. Spring has a basic one for testing purposes if you're already using Spring. Apache DBCP is all about providing a pooling DataSource. Pretty much any other JDBC connection pooling library out there will also provide a DataSource implementation. Just instantiate it, set the properties, and run with it.
Related
I have a java REST API application using Quarkus as the framework. The application uses a PostgreSQL database, which is configured via the application.properties config file for hibernate entities (using "quarkus-hibernate-orm" module) etc. However, there are cases where i will have to dynamically connect to a remote database (connection info will be supplied by parameters) to read and write data from during runtime as well. How do i go about this the best way with Quarkus? For simplicity reasons we can assume that the remote databases are of the same type (PostgreSQL) so we don't have to worry about whether the correct driver is locally available or not.
Is there something provided by Quarkus or the environment to establish these connections and read/write? i dont need an ORM layer here necessarily, as i may not know the structure beforehand either. Simple queries are also sufficient. When i try to research this subject i can only get information about static hibernate or datasource configurations in Quarkus, but i won't know what they look like beforehand. Basically, is there some kind of "db connection provider" etc. i should use or do i simply have to manually create new plain JDBC connections in my own code for it?
I have a requirement where I need to create a jpa entity manager instance using plain jdbc connection. more precisely I have a jdbc connection and from that I want to create an entitymanager instance.
JPA is java programming interface specification that describes the management of relational data in applications using Java Platform, Standard Edition and Java Platform, Enterprise Edition. So you need some implementation api's like hibernate, Spring repo etc to make your application JPA enabled.
You need to use connection parameters or JNDI Datasource name in the definition of your persistence unit.
As for using your own JDBC connection, the EntityManagerFactory implementation would need to know how to do this, and I doubt any implementations (hibernate or others) are meant to work the way your want it.
Maybe implement a datasource which returns the connection any way you like.
So, I have a Java EE application using Spring framework and JDBCtemplate. And, my application has to do several JDBC database read requests (no/very little writes) on the same database (which is a Postgres DB but is not normalized for a bunch of reasons) but with different sql statements (different where clauses). So, given this situation, I would like to be able to cache the database and be able to run queries on the cache, thereby saving me expensive JDBC calls. So, please suggest appropriate tools or frameworks or any other solutions.
You can start with using simple maps depending the query parameter you are using. A more viable solution is using ehcache.
If you use Spring 3.1 or later, you can use #Cacheable on methods. You need to include <cache:annotation-driven /> in your application context configuration. For simple cases you may use spring's ConcurrentCacheFactoryBean as cache manager. For more complex cases you can use ehcache via spring's ehcache adapter. Use #CacheEvict to reset cache.
I have created a simple JSF application, now must to connect to SQL Server and perform CRUD operations on those tables from database.
I was a .NET programmer and i don't know how to connect to SQL Server from JSF. I have read something about JNDI, but not understood well. My questions are:
where should JNDI be defined: on Tomcat or my application?
where to define the connection string?
which driver/jar should be used?
Can you recommend any code samples, links to tutorials how to perform crud operations, or any other guidance?
where should JNDI be defined: on Tomcat or my application?
In the JNDI container. That's thus Tomcat.
where to define the connection string?
In the JNDI container. In case of Tomcat, that'll go in the context.xml. You can either modify Tomcat's own context.xml or supply your own in META-INF folder of your webapp. More details can be found in the Tomcat JNDI resources HOW-TO.
which driver/jar should be used?
The one which can communicate with the DB in question. In case of Microsoft SQL Server, that's under each the DB-vendor provided JDBC driver or the performancetechnically better jTDS driver.
Here are some useful tutorials which might help you step by step further:
Basic DAO tutorial (with JDBC) - Using in JSF
CRUD with JSF 2.0
JPA tutorial (better than JDBC)
Netbeans CRUD with JSF 2.0 and JPA 2.0
This is a very broad question. I will take a shot at trying too keep it simple and short.
Here are the steps.
First create a backing bean that works with your front end face page.
Create a service class that encapsulates the CRUD tasks.
Create a database methods class that performs each CRUD task.
This is how the code should flow:
"Your UI face invokes a method in the backing bean->the backing bean invokes service class->service invokes the database methods class. This is commonly referred to as the DAO pattern."
For details on how to connect to a database.
You can either create a local data source and connect via standard JDBC procedure.
Or you can create connection pools in your container (JBOSS, WebLogic, etc). Then look up those connection pools in your app via JNDI lookup.
If you are very new to this, then I would recommend to start out with creating a basic database connection using JDBC and running your queries against it. In the long term, you will want to get yourself familiar with connection pool (actually this will give you a better performance too), Spring JDBC framework, ORM support (hibernate, iBatis).
Here is a link to starting a jdbc connection for Microsoft SQL server (example on step1).
Is it safe to run a database connection pool (like Commons DBCP or c3p0) as part of an application deployed to an application server like Glassfish or Websphere? Are there any additional steps over a standalone application that should be taken to ensure safety or performance?
Update, clarification of reason - the use case I have in mind could need new data sources to be defined at runtime by skilled end users - changing the data sources is part of the application's functionality, if you like. I don't think I can create abnd use container-managed pools on the fly?
AFAIK it works, but it will of course escape the app. server management features.
Also, I'm not entirely sure how undeployment or redeployment happens and whether the connections are correctly disposed. But that can be considered as a minor safety detail: if disposed improperly, connections will simply time out I guess. I'm also not entirely sure whether it works for XA data source which integrates with the distributed transaction manager.
That said, using the app. server pool is usually a matter of configuring the JNDI name in a configuration file. Then you get monitoring, configuration from the admin console, load management, etc. for free.
In fact, you can create container-managed datasources depending on AS you use.
For example, Weblogic has an extensive management API that is used, for example, by their own WLST (Weblogic Shell) to configure servers by scripts. This is, of course, Java API. It has methods to create and configure datasources too.
Another route is JMX-based configuration. All modern AS expose themselves as JMX containers. You can create datasources via JMX too.
All you need is to grant your application admin privileges (i.e. provide with username/password).
The benefit of container-managed DS is it can be clustered. Also, it can be managed by human being using standard AS UI.
If that doesn't work for you, why, sure you can create application-managed DS any time and in any numbers. Just keep in mind that it will be bound to a specific managed server (unless you implement a manual clustering of it's definition).
I don't see why you'd want to. Why not use the connection pool that the app server provides for you?
Update: I don't believe it's possible to create new pools on the fly without having to bounce the app server, but I could be wrong. If that's correct, I don't believe that Commons DBCP or C3P0 will help.