Connect to local MySQL database in JBoss EWS 2.0 - mysql

I created an OpenShift application (Tomcat 7 (JBoss EWS 2.0)) and with the help of developercorey I was able to deploy my application to OpenShift and connect to the the local MySQL database in OpenShift.
I am now trying to connect to the same database only on my local machine but when I call initialContext.lookup("jdbc/local") it throws an error. It's acting like its unable to find the context.xml file even though when the application is deployed in OpenShift it works correctly.
Exception: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initialNeed to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
Exception: java.lang.NullPointerExceptionnull
I am used to Tomcat and the location for datasources is located in:
WebContent\META-INF\context.xml
But for JBoss the context.xml is located in:
.openshift\config\context.xml
Is there additional mapping I need to do to make the application connect locally?
Thanks in advance!
private static DataSource getDataSource() {
DataSource datasource = null;
try {
InitialContext ic = new InitialContext();
Context initialContext = (Context) ic.lookup("java:comp/env");
datasource = (DataSource) initialContext.lookup("jdbc/local");
} catch (Exception ex) {
System.out.println("Exception: " + ex + ex.getMessage());
} finally {
return datasource;
}
}
<Resource name="jdbc/OpenShift"
url="jdbc:mysql://127.10.198.2:3306/remote"
driverClassName="com.mysql.jdbc.Driver"
username="root"
password="*****"
auth="Container"
type="javax.sql.DataSource"
maxActive="20"
maxIdle="5"
maxWait="10000"
/>
<Resource name="jdbc/local"
url="jdbc:mysql://127.0.0.1:3306/local"
driverClassName="com.mysql.jdbc.Driver"
username="root"
password="******"
auth="Container"
type="javax.sql.DataSource"
maxActive="20"
maxIdle="5"
maxWait="10000"
/>

You are correct. When working on OpenShift, the path you need to update the context.xml file is indeed .openshift\config\context.xml
However, if you're working locally. You need to update the context.xml under the following path - $CATALINA_BASE/conf/context.xml

Related

Websphere Liberty 18.0.0.3 MySQL data source object not injected

My web app is not getting the datasource which was configured in server.xml. I have added the sqlconnector jar (mysql-connector-java-8.0.12) under the folder C:\wlp\usr\shared\resources\mysql
server.xml
<!-- Enable features -->
<featureManager>
<feature>cdi-1.2</feature>
<feature>jaxrs-2.0</feature>
<feature>jdbc-4.0</feature>
<feature>jndi-1.0</feature>
<feature>jpa-2.0</feature>
<feature>localConnector-1.0</feature>
<feature>servlet-3.1</feature>
</featureManager>
<!-- Declare the jar files for MySQL access through JDBC. -->
<library id="MySQLLib">
<fileset dir="${shared.resource.dir}/mysql" includes="mysql-connector-java-8.0.12.jar"/>
</library>
<!-- Declare the runtime database -->
<dataSource jndiName="AdminWeb/jdbc/AdminDS" transactional="false">
<jdbcDriver libraryRef="MySQLLib"/>
<properties databaseName="admin" password="****" portNumber="3306" serverName="localhost" user="root"/>
</dataSource>
DAO
#Resource(name = "AdminWeb/jdbc/AdminDS",lookup="AdminWeb/jdbc/AdminDS")
DataSource dataSource;
public UserEntity getAllUsers() {
UserEntity user = new UserEntity();
Connection connection = null;
try {
System.out.println("****************1");
connection = dataSource.getConnection();
System.out.println("2");
While invoking the webapp, the getconnection method throws
[ERROR ] SRVE0777E: Exception thrown by application class 'com.fist.tools.admin.dao.UserDAO.getAllUsers:25'
java.lang.NullPointerException
Could anyone please help me on this?
The dataSource/server configuration itself looks fine. #Resource can only be injected into web components/ejb components. Does the class you are injecting into fit that description?

javax.naming.NoInitialContextException Tomcat DataSource JNDI intellij idea 14

I am trying go get Connection Pool with Tomcat DataSource and MySQL in ntellij idea 14. I've done these steps:
Select "Project Structure" from the File menu
From there, select the "Facets" option. Make sure that you have a Web
facet configured. If not, add one.
Once the web facet is added, select "Add Application Server specific
descriptor..."
Select Tomcat Context Descriptor from the options and click OK.
I got META-INF/context.xml in web directory. I added next lines in context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/payments"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/paymentsdb"
username="root"
password="password"
maxActive="100"
maxIdle="20"
minIdle="5"
maxWait="10000"/>
</Context>
And this into web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!--DB Connection start-->
<description>MySQL Test App</description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/payments</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
This is my method to get connection
public static Connection createConnection(){
Context context = null;
DataSource dataSource = null;
Connection connection = null;
try {
context = (Context) new InitialContext().lookup("java:comp/env");
dataSource = (DataSource) context.lookup("jdbc/payments");
connection = dataSource.getConnection();
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
I got this exception in line
dataSource = (DataSource)
context.lookup("java:comp/env/jdbc/payments");
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:350)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at ua.epam.kuts.dao.MySQLDAOFactory.createConnection(MySQLDAOFactory.java:22)
at Test.main(Test.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
I use Tomcat 8.0.23. Any idea? I googled for several hours and didn't find anything that helped me. Drew me mad. I checked connection with Statment and it works. I have connector.jar.
I've done stupid mistake. I was trying to test DataSource connection running it in public static main(String[] args) method instead of running it on the server. That's why InitialContext was not initialized. Found answer here in second post.

Flyway migration, Unable to obtain Jdbc connection from DataSource

I am trying to use flyway to create and manage a MySQL database. Here is the code i have got so far.
FlywayMigration.java : Class that applys the migration
public class FlywayMigration
{
public FlywayMigration(DatabaseConfiguration configuration, Flyway flyway)
{
flyway.setDataSource(configuration.getDataSource());
flyway.migrate();
}
public static void main(String[] args)
{
new FlywayMigration(new DatabaseConfiguration("database.properties"), new Flyway());
}
}
DatabaseConfiguration.java : Configuration class, this class will configure the datasource to be applyed to the Flyway.setDataSource method
public class DatabaseConfiguration
{
private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
private PropertiesUtil prop = null;
public DatabaseConfiguration(String file)
{
prop = new PropertiesUtil(file);
}
public String getDataSourceClass()
{
return prop.getProperty("mysql.data.source.class");
}
public String getURL ()
{
return prop.getProperty("mysql.url");
}
public String getHostName()
{
return prop.getProperty("mysql.host.name");
}
public String getDatabaseName()
{
return prop.getProperty("mysql.database.name");
}
public DataSource getDataSource()
{
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setURL(getURL());
dataSource.setUser(prop.getProperty("mysql.user.name"));
dataSource.setPassword(null);
return dataSource;
}
}
database.properties is the file where i store the database information, password can be null
mysql.data.source.class=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/vmrDB
mysql.host.name=localhost
mysql.database.name=vmrDB
mysql.user.name=root
And i get the folowing error in my trace
Exception in thread "main" org.flywaydb.core.api.FlywayException: Unable to obtain Jdbc connection from DataSource
at org.flywaydb.core.internal.util.jdbc.JdbcUtils.openConnection(JdbcUtils.java:56)
at org.flywaydb.core.Flyway.execute(Flyway.java:1144)
at org.flywaydb.core.Flyway.migrate(Flyway.java:811)
at com.bt.sitb.vmr.migration.FlywayMigration.<init>(FlywayMigration.java:10)
at com.bt.sitb.vmr.migration.FlywayMigration.main(FlywayMigration.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Can someone please tell me why the DataSource from MySQL is not connecting?
It looks like Flyway cannot connect to the database.
One reason for this is that the database in the database URL does not exist.
Question: does your database schema exist?
If your answer is no, then:
connect to jdbc:mysql://localhost:3306/mysql
also specify the schema to use for migration with flyway.setSchemas(configuration.getDatabaseName())
you also need flyway.init() before you can initialize migration of your database.
Ran into this same issue. Apparently, the problem was with my .properties file. The jar was using the one packaged with it and not the external one. So I moved my external properties file out of the resources folder and into the root directory of the jar and problem solved!
Hope this helps someone.
I had this same issue when working on a Java application in Debian 10 using Tomcat Application server.
I defined the connection strings for the database in the context.xml file, however, when I start out the application and try to log into the application, I get the error:
Exception in thread "main" org.flywaydb.core.api.FlywayException: Unable to obtain Jdbc connection from DataSource
at org.flywaydb.core.internal.util.jdbc.JdbcUtils.openConnection(JdbcUtils.java:56)
at org.flywaydb.core.Flyway.execute(Flyway.java:1144)
Here's what I figured out:
I finally realized that the application was using internally defined database connection strings that were packaged with it. The internally defined database connection strings were different from my own database connection strings defined in the context.xml file.
The solution for me was to either modify the internally defined database connection strings that were packaged with the application or use the same internally defined database connection strings that were packaged with application in my context.xml file.
That's all.
I hope this helps.

Testing EJB 3.1 with JUnit gets error Invalid resource

I am trying to test an Stateless bean with JUnit in netbeans. This bean uses an EntityManager.
#Stateless
public class myEjb{
#PersistenceContext
private EntityManager em;
public MyResult getResult(){
return em.find(...);
}
}
Then I write a test class.
public class myTest{
private static EJBContainer ec;
private static Context ctx;
#BeforeClass
public static void setUpClass(){
ec = EJBContainer.createEJBContainer();
ctx = ec.getContext();
}
....
}
When I run the test, it does not work. I obtain the following message:
Invalid resource : mydb__pm
The error occurs when this line is executed:
ec = EJBContainer.createEJBContainer();
If a change my bean by removing the entity manager, it works. So, it seems that I have a problem with the entity manager.
My persistence.xml file is simple:
<persistence version="2.0" ...>
<persistence-unit name="MetisDemoPU" transaction-type="JTA">
<jta-data-source>MyDb</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
</persistence>
Finally, I create a JSF managed bean that called my EJB (which uses the entity manager) and it works.
#ManagedBean
#RequestScoped
public class myManagedBean{
#EJB
private OfferEjb offerEjb;
...
}
Any help would be appreciated!
Ok, I find a solution to my problem. I am now able to use JUnit to test my session bean with a persistence context.
I am not a specialist, so my explanation will probably not be complete.
With netbeans 7.2, there is an embedded glassfish server which is used for the test. It is necessary to configure the jdbc parameters in the domain.xml file and then for me it works.
On my computer, this file is under
C:\Program Files\glassfish-3.1.2.2\glassfish\domains\domain\
I just add a jdbc connection pool and a jdvc jndi.
This article contains more details.

Jetty - set system property [duplicate]

This question already has answers here:
How to portably read configuration data from a servlet
(4 answers)
Closed 7 years ago.
I run webapp on Jetty. The configuration for the app come from file that lives on the same server where Jetty is running. Inside the app I rely on the system property to obtain path to the file so I can parse it. E.g.
final String loc = System.getProperty(FACTORY);
Now I can start jetty with D switch to provide $FACTORY on the command line but I rather put it in jetty.xml if I can. I know there is <SystemProperty /> tag but that seems to just provide system value that already exists for the <Set/> tag. Can someone give me example how this can be achieved? (If it can be achieved)
For the record, if you really need to do this through system properties (I did) you can do this to append for example -Drun.mode=staging to the system properties:
<Call class="java.lang.System" name="setProperties">
<Arg>
<New class="java.util.Properties">
<Call name="putAll">
<Arg><Call class="java.lang.System" name="getProperties"/></Arg>
</Call>
<Call name="setProperty">
<Arg>run.mode</Arg>
<Arg>staging</Arg>
</Call>
</New>
</Arg>
</Call>
... and yes you can probably can program your application through this ;-)
If you're starting Jetty through its Java API for a testing or 'embedded' application, the following example shows actually setting Java System properties prior to the startup of your WebAppContext.
private void startJetty() {
try {
long startTime = System.currentTimeMillis();
server = new Server();
setUpSystemProperties(server);
Connector connector = new SelectChannelConnector();
connector.setPort(port);
server.addConnector(connector);
WebAppContext webAppContext = new WebAppContext();
webAppContext.setWar("src/main/webapp");
server.setHandler(webAppContext);
server.start();
}
catch (Exception e) {
throw new RuntimeException("Failed to set-up web server fixture", e);
}
}
private void setUpSystemProperties(Server jettyServer) {
final Properties systemProperties = new Properties();
// set your system properties...
systemProperties.setProperty("yourProperty", "yourValue");
jettyServer.addLifeCycleListener(new SystemPropertiesLifeCycleListener(systemProperties));
}
private class SystemPropertiesLifeCycleListener extends AbstractLifeCycleListener {
private Properties toSet;
public SystemPropertiesLifeCycleListener(Properties toSet) {
this.toSet = toSet;
}
#Override
public void lifeCycleStarting(LifeCycle anyLifeCycle) {
// add to (don't replace) System.getProperties()
System.getProperties().putAll(toSet);
}
}
Unlike most of these answers, I won't lecture you about whether this is 'proper' compared to JNDI or some other technology you didn't ask about.
I'm going to accept #vanje answer since it got me thinking into right direction. Here's what I ended up using:
Create jetty-web.xml outside of your WAR distro (no you don't want to package it with WAR if you want to configure the app from "outside")
Place jetty-web.xml alongside of jetty.xml
I needed just a single parameter so I ended up with the following:
jetty-web.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN"
"http://jetty.mortbay.org/configure.dtd">
<Configure class="org.mortbay.jetty.webapp.WebAppContext">
<New class="org.mortbay.jetty.plus.naming.EnvEntry">
<Arg>myOwnParam</Arg>
<Arg type="java.lang.String">//some/path/to/the/file</Arg>
</New>
</Configure>
Java snippet
InitialContext c = new InitialContext();
EnvEntry env = (EnvEntry)
c.lookup("org.mortbay.jetty.plus.naming.EnvEntry/myOwnParam");
final String myString = (String) env.getObjectToBind();
The biggest gotcha for me here was that I was trying to get myString from the defaul env which didn't work until I realized that JNDI was using local context. This is OK for me but will break portability if you try to move WAR on say Tomcat. If someone can post an example how this can be saved into default context that would be greatOwnParam
To configure a web application it is better to avoid system properties and to use JNDI instead.
Recently I posted an example on how to accomplish that with Jetty.