Deploy an app in Payara using existing database in MSSQL - sql-server

I am totally new to Payara and I have question about how can I deploy an app which uses existing database? So I have .war file and .bak file (MSSQL backup).
Thanks a lot.

In my opinion you're a little bit confused about Payara capabilities. Payara is an Application Server based on Glassfish, it doesn't matter the kind of database that any of your deployed applications has to access and of course the database should exists.
For MSSQL database you could use Microsoft JDBC driver. There's an excellent tutorial here:
On the other hand, it possible to configure a database connection pool in Payara/Glassfish server.
In the official Payara's blog there are interesting posts and one focused on database connection pool that can be found here .
Don't worry if the article is talking about a derby database, you only has to modify Microsoft JDBC params properly when configurating the pool.
In addition, if you decide to use connection pool it's necessary to made some changes inside your application:
Change method getConnection() from DRIVER_MANAGER (jdbc) to DATASOURCE
DRIVER_MANAGER:
public Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
Properties properties = new Properties();
properties.setProperty("user", DB_USER);
properties.setProperty("password", DB_PWD);
properties.setProperty("useSSL", "false");
properties.setProperty("autoReconnect", "true");
if (connection == null || connection.isClosed()) {
connection = DriverManager
.getConnection("jdbc:mysql://"+DB_HOST+":"+DB_PORT+"/" + SCHEMA.substring(0, SCHEMA.length() - 1), properties);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
DATA_SOURCE:
public Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
Properties properties = new Properties();
properties.setProperty("user", DB_USER);
properties.setProperty("password", DB_PWD);
properties.setProperty("useSSL", "false");
properties.setProperty("autoReconnect", "true");
InitialContext ctx;
ctx = new InitialContext();
ds = (DataSource) ctx.lookup("database_resource_pool");
if (connection == null || connection.isClosed()) {
connection = ds.getConnection();
}
} catch (NamingException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
Add resource-ref to web.xml
<resource-ref>
<res-ref-name>database_db_pool</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
Add resource-ref to WebContent/WEB-INF/glassfish-web.xml
<resource-ref>
<res-ref-name>database_db_pool</res-ref-name>
<jndi-name>database_db_pool</jndi-name>
</resource-ref>

Related

Can database Snapshots be passed to other methods?

I have tried to write this code in three different ways but none of them work correctly.
I did not want to write any business logic code in the class that opens and closes the database, so I have a database class. I am trying to take a Snapshot of the database to send it to the business logic class.
However what is the correct way to send a Snapshot or Iterator across to another class?
Here is my current code in the database connection class trying to return an Iterator to the business logic class:
public synchronized DBIterator getSnapShot() throws IOException {
ReadOptions ro = new ReadOptions();
Options options = new Options();
DBIterator iterator = null;
try {
DB database = (factory.open(new File("myFilePath"), options));
ro.snapshot(database.getSnapshot());
iterator = database.iterator(ro);
return iterator;
} catch (Exception e) {
e.printStackTrace();
} finally {
getLinkPacDatabase().close();
}
return iterator;
}
Then I try to iterate in the business logic class like this:
for (iterator.seekToFirst(); iterator.hasNext(); iterator.next()) {
I have also tried to grab a Snapshot only from the database like this, but it also does not work because the database connection has to close obviously:
public synchronized Snapshot generateSnapshot() {
Snapshot snapshot = null;
DB database = null;
try {
database = factory.open(new File("myFilePath"), options);
snapshot = database.getSnapshot();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
database.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return snapshot;
}
There is no documentation for the levelDb Java port library I am using, therefore I am hoping someone can explain if I am using Snapshots incorrectly.

Solr : Server at http://localhost:8080//solr returned non ok status:500, message:Internal Server Error

I keep getting this error "Server at solr 8080 returned non ok status:500, message:Internal Server Error" when I am trying to index text files on solr server using solrj api.
My code is as follows:
public void IndexData(String filePath,String solrId)
{
try {
String urlString = "http://localhost:8080//solr";
HttpSolrServer server = new HttpSolrServer(urlString);
ContentStreamUpdateRequest up
= new ContentStreamUpdateRequest("/update/extract");
up.addFile(new File(filePath),"");
up.setParam("literal.id", solrId);
up.setParam("uprefix", "attr_");
up.setParam("fmap.content", "attr_content");
up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true,true);
server.request(up);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SolrServerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
I am able to query the solr server using same server but while indexing data, why I am getting this error?
log4j:WARN No appenders could be found for logger (org.apache.solr.client.solrj.impl.HttpClientUtil).
log4j:WARN Please initialize the log4j system properly.
org.apache.solr.client.solrj.impl.HttpSolrServer$RemoteSolrException: Server at `http://localhost:8080//solr` returned non ok status:500, message:Internal Server Error
at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:372)
at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:180)
at dataAnalysis.dataIndexer.DataIndexer.IndexData(DataIndexer.java:41)
at dataAnalysis.dataHome.DataHome.main(DataHome.java:13)
Hey great news I was able to solve the issue. Two changes required. First I edited solrconfig.xml to remove extra tab like to for every path and second change was to copy jars from /solr/contrib/extraction/lib to /tomcat/web-inf/lib folder. –

ORA-12505, TNS:listener does not currently know of SID given in connect descriptor

I'm very new on Database subjects and trying to do some beginner stuff with connecting from Netbeans. but when i test my connection i'm facing with some error (i searched this error but couldnt understand anything). i installed Oracle 10g Express Database Edition and downloaded ojdbc6.jar for Oracle Thin. this my simple code just for test the driver version;
import java.sql.*;
import oracle.jdbc.pool.OracleDataSource;
class JDBCVersion
{
public static void main (String args[]) throws SQLException
{
OracleDataSource ods = new OracleDataSource();
ods.setURL("jdbc:oracle:thin:#localhost:1521:XE [hr on HR]");
Connection conn = ods.getConnection();
// Create Oracle DatabaseMetaData object
DatabaseMetaData meta = conn.getMetaData();
// gets driver info:
System.out.println("JDBC driver version is " + meta.getDriverVersion());
}
}
and i get
Exception in thread "main" java.sql.SQLException: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
as i said i am newbie about this database stuff and i will be very glad if you can help with explaining. thanks anyway
Here is the code on how to connect to your Oracle Database:
public class JDBCVersion {
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
return;
}
Connection connection = null;
try {
connection = DriverManager.getConnection(
"jdbc:oracle:thin:#localhost:1521:XE", "username",
"password");
} catch (SQLException e) {
e.printStackTrace();
return;
}
}
Where username and password have to be changed accordingly. Also if you have installed Oracle 10g then the right version would be ojdbc14.jar.

database connection on local machine

I was originally coding on a server with Apache installed and used this function to connect to the database
public Connection getDBConnection()
{
java.sql.Connection conn=null;
//synchrnized(this)
try
{
DriverManager.registerDriver(new OracleDriver());
conn = DriverManager.getConnection("jdbc:oracle:thin:#dukeorrac01:1521:ORDB1","nrsc","nrsc");
}
catch (Exception e)
{
e.printStackTrace();
}
return conn;
}
Probably not the best way to do it, but it worked. However, I've moved to coding on my local machine and I was given jboss as an IDE to work with. I've gotten everything working (sorta) except, it seems to be connecting to a different databse. I get exhausted resultsets (which isn't right). I "thought" the "getConnection("jdbc:oracle....") was what established the connection to the actual database.
Why is this not working on my local machine when it worked perfectly fine on the remote server?
P.S. I'm new to database/server configuration stuff. So, don't assume I know some step in setting up a database or server. Also, I did not create this orginally. It was given to me to use.
Thanks
The error seems to indicate that you are not closing your result sets properly. In the following example see how after a statement is executed you have to close those resources in the finally block.
W.r.t why you did not see this error before is probably because your server probably had lots of resources whereas your local machine has limited resources and your default settings are not modified to reflect the needs of your application.
In general, its better to do basic house keeping like closing all the open result sets and statements whether or not you have enough resources allocated on your DB.
public void getDBConnection() {
Connection conn = null;
Statement statement = null;
ResultSet rs = null;
try {
connection = DriverManager.getConnection("jdbc:oracle:thin:#dukeorrac01:1521:ORDB1","nrsc","nrsc");
statment = connection.createStatement();
statement.setFetchSize(Integer.MIN_VALUE);
// Do more stuff, iterate to ResultSet etc...
} catch (SQLException ex) {
// Exception handling stuff
...
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) { /* ignored */}
}
if (statment != null) {
try {
statment.close();
} catch (SQLException e) { /* ignored */}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) { /* ignored */}
}
}
}

Using Silverlight 4 WebClient OpenReadCompleted throughs Security Exceptions on hosted web server. What's the pronblem?

Seems to be a simple thing using a WebClient object to retrieve content of a text file in a Silverlight web application.
It works in our intranet setup on a local server, but when deployed on a hosted "hostgator" server, WebClient gets a security exception. Following suggestions from numerous web blogs, I tried crossdomain access policy and crossdomain.xml with no result - no wonder, all in on the same domain.
private void LoadCSVFile(string csvFile)
{
try
{
WebClient client = new WebClient();
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
client.OpenReadAsync(new Uri(csvFile, UriKind.Absolute));
}
catch (Exception ex)
{
string errorMessage = String.Format("Ex accessing csv: {0}\n{1}", csvFile,
}
}
//the csvFile is an absolute path: http://myserver.com/myapplication:port/sourcefiles/file.csv
//The sourcefiles is a virtual directory in the same website containing csv files
void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null)
{
try
{
using (System.IO.StreamReader reader = new System.IO.StreamReader(e.Result))
{
mainFileData = new List<string>();
string line;
}
}
catch (Exception ex)
{
string errorMessage = ex.InnerException;
}else
{
"here is the place for the exception!!!!!!! so, e.Error is not null"
}
}
The text of the exception reads: System.SecurityException: Security error at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) etc.
Spend 2 days gooogling with no result.
Please help.
V
A couple of things to check:
(1) The format for the URL that you list (http://myserver.com/myapplication:port/sourcefiles/file.csv) looks odd. Shouldn't it be http://myserver.com:port/myapplication/sourcefiles/file.csv? Or is that just a typo?
(2) What's the port number that you're accessing? The Silverlight WebClient will only access ports 80 and 443.
(3) Can you get to the file using a direct URL? Sometimes web servers need to be told about a specific file type before they'll allow it to be served up.

Resources