Connection not established after database backup - oracle

Deployed web application in weblogic 12c server and it has data source configure to make use of connection pool.
Database is Oracle 12,
Usecase: when application is alive up and running, for a min database backup script has been run(stop db, backup, start db), after that when trying to access application(session got established) then got SQL error (connection already closed). What could be an issue?
Temporary solution: after restarting application it was working fine with out any issue. Still wondering how it got worked?
Datasource configuration :
**dataSource {
configClass = GrailsAnnotationConfiguration.class
dialect = "org.hibernate.dialect.Oracle10gDialect"
loggingSql = false
jmxExport = false
}
hibernate {
cache.use_second_level_cache = true
cache.use_query_cache = true
cache.provider_class = 'net.sf.ehcache.hibernate.EhCacheProvider'
hbm2ddl.auto = null
show_sql = false
// naming_strategy = "org.hibernate.cfg.ImprovedNamingStrategy"
dialect = "org.hibernate.dialect.Oracle10gDialect"
config.location = [
"classpath:hibernate-core.cfg.xml",
"classpath:hibernate-utility.cfg.xml"
]
}
// environment specific settings
environments {
development {
dataSource {
}
}
test {
dataSource {
}
}
production {
dataSource {
}
}
}**

Related

Exception when Implementing RPC Security Management in corda using spring webserver

Spring Web server not started when we specify RPC Security Management configuration in node.conf file.
Getting error as Unresolved reference: proxy, while running PartyAServer.
Below is my server configuration for PartyA node :-
task runPartyAServer(type: JavaExec) {
classpath = sourceSets.main.runtimeClasspath
main = 'com.example.server.Server'
environment "server.port", "10022"
environment "config.rpc.host", "localhost"
environment "config.rpc.port", "10006"
}
I am able to start the node with following node A configuration, but facing error while running PartyA server.
node {
name "O=PartyA,L=London,C=GB"
advertisedServices = ["com.example"]
p2pPort 10005
rpcPort 10006
cordapps = ["$corda_release_group:corda-finance:$corda_release_version",
"com.example:java-source:$version",
"com.example:base:$version"]
}
Below is my node.conf for PartyA node :-
extraAdvertisedServiceIds=[
"com.example"
]
myLegalName="O=PartyA,L=London,C=GB"
networkMapService {
address="localhost:10002"
legalName="O=Controller,L=London,C=GB"
}
p2pAddress="localhost:10005"
rpcAddress="localhost:10006"
rpcUsers=[]
security = {
authService = {
dataSource = {
type = "DB",
passwordEncryption = "SHIRO_1_CRYPT",
connection = {
jdbcUrl = "jdbc:oracle:thin:#172.16.105.21:1521:SFMS"
username = "abinay"
password = "abinay"
driverClassName = "oracle.jdbc.OracleDriver"
}
}
options = {
cache = {
expireAfterSecs = 120
maxEntries = 10000
}
}
}
}
Without having username and password how nodeRPCConnection(proxy) will be established with following code,
#PostConstruct
public void initialiseNodeRPCConnection() {
NetworkHostAndPort rpcAddress = new NetworkHostAndPort(host,rpcPort);
CordaRPCClient rpcClient = new CordaRPCClient(rpcAddress);
rpcConnection = rpcClient.start(username, password);
proxy = rpcConnection.getProxy();
staticMap.put("proxy",proxy);
}
This strikes me as more likely to be an oracle connection issue.
I'd start by writing some java code just to ensure that you can connect to the oracle DB, and then focus on getting that to work in the cordapp environment.
There's a good developer example on this (in both java and kotlin: https://github.com/corda/samples-java/tree/master/Basic/flow-database-access ; https://github.com/corda/samples-kotlin/tree/master/Basic/flow-database-access)
Best of luck on this!

ASP.NET CORE: Change connectionString through WEBAPI

I have declared connection string in appsetting.json file. I would like to change the connection string through web api call.
Is there a way to do it?
I am working with ASP.Net Core.
appsettings.json:
"Database": {
"ProviderName": "MySQL",
"ConnectionString": "server=localhost;database=sampledb;uid=user;pwd=user"
},
Startup.cs
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
//Accessing Database section from appsettings.json
services.Configure<dbSettings>(StaticSettings.GetSection("Database"));
Firstly, in development environment the right place to store connection string is in User Secrets. In Visual Studio, Right click on Project > Manage User Secrets, opens secrets.json. User Secrets option works only on your dev. machine.
While if your app is running on Azure, Azure Portal has application settings options to define connection strings for your app.
Coming to your question, you can store multiple connection strings in application settings. For example:
"ConnectionStrings": {
"dev_db": "Server=devSQLServerName;Database=dev;User Id=userName;Password=yourPassword",
"test_db": "Server=testSQLServerName;Database=test;User Id=userName;Password=yourPassword",
"staging_db": "Server=stagingSQLServerName;Database=staging;User Id=userName;Password=yourPassword",
"production_db": "Server=prodSQLServerName;Database=staging;User Id=userName;Password=yourPassword"
}
Now, write a method to get the connection string based on your environment.
public string GetDBConnectionString(string environment)
{
string connectionString = string.Empty;
if (environment == "production")
{
connectionString = Configuration["ConnectionStrings:production_db"];
}
else if (environment == "development")
{
connectionString = Configuration["ConnectionStrings:dev_db"];
}
else if (environment == "test")
{
connectionString = Configuration["ConnectionStrings:test_db"];
}
else if (environment == "stage")
{
connectionString = Configuration["ConnectionStrings:staging_db"];
}
if (connectionString == null)
{
throw new Exception("Could not locate production DB connection string for env: " + environment );
}
else
{
return connectionString;
}
}
Now call this method from your Web API's action method or where ever you want to consume.

multiple times triggering of Datasource.groovy in grails application ( throwing exception when called second time)

Work Around for creating the application :
creating a Grails Application using Postgres database.
Needs to create Database when the application is executed (i.e database is to be created from the project itself instead of creating the database manually).
For creating the database I am calling a groovy service CreateDatabaseService in datasource.groovy as follows :
import demo.grails.CreateDatabaseService
CreateDatabaseService.serviceMethod()
dataSource {
pooled = true
jmxExport = true
driverClassName = "org.postgresql.Driver"
dialect = "org.hibernate.dialect.PostgreSQLDialect"
username = "postgres"
password = "password"
}
hibernate {
cache.use_second_level_cache = true
cache.use_query_cache = false
singleSession = true // configure OSIV singleSession mode
flush.mode = 'manual'
}
// environment specific settings
environments {
development {
dataSource {
dbCreate = "create-drop"
url = "jdbc:postgresql://localhost:5432/SampleAppDb"
}
}
test {
dataSource {
dbCreate = "update"
url = "jdbc:postgresql://localhost:5432/SampleAppDb"
}
} .....
Full code for CreateDatabaseService :
package demo.grails
import groovy.sql.Sql
import java.sql.DriverManager
import java.sql.Connection
import java.sql.SQLException
import java.sql.*
class CreateDatabaseService {
public static flag =0
def static serviceMethod() {
Connection conn = null;
Statement stmt = null;
try{
if(flag == 0) {
//STEP 2: Register JDBC driver
Class.forName("org.postgresql.Driver");
//STEP 3: Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432", "postgres", "password");
//STEP 4: Execute a query
System.out.println("Creating database...");
stmt = conn.createStatement();
String isDatabase = "select datname from pg_catalog.pg_database where lower(datname) = lower('SampleAppDb')"
ResultSet rs = stmt.executeQuery(isDatabase);
String idr
while(rs.next()){
//Retrieve by column name
idr = rs.getString("datname");
}
if(!idr.equals("SampleAppDb")) {
String sql = "create database SampleAppDb";
stmt.executeUpdate(sql);
System.out.println("Database created successfully...");
flag =1
}
else {
flag =1
}
}
}catch(Exception e){
e.printStackTrace();
}
finally{
stmt.close();
conn.close();
}
}
}
On running the grails app following is the full stack trace that shows datasource.groovy is called multiple times, though the database is created from the application but complains about no suitable driver found when datasource.groovy is called second time.
|Loading Grails 2.4.5
|Configuring classpath
.
|Environment set to development
.................................
|Packaging Grails application
................Connecting to database...
Creating database...
Database created successfully...
....................
|Running Grails application
Connecting to database...
Error |
java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost:5432
Error |
at java.sql.DriverManager.getConnection(DriverManager.java:689)
Error |
at java.sql.DriverManager.getConnection(DriverManager.java:247)
Error |
at java_sql_DriverManager$getConnection.call(Unknown Source)
Error |
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
Error |
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
Error |
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:124)
Error |
at demo.grails.CreateDatabaseService.serviceMethod(CreateDatabaseService.groovy:23)
Error |
at demo.grails.CreateDatabaseService$serviceMethod.call(Unknown Source)
Error |.......
Please suggest how to overcome this error.
BuildConfig.groovy
dependencies {
// specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes e.g.
// runtime 'mysql:mysql-connector-java:5.1.29'
// runtime 'org.postgresql:postgresql:9.3-1101-jdbc41'
runtime 'postgresql:postgresql:9.0-801.jdbc4'
test "org.grails:grails-datastore-test-support:1.0.2-grails-2.4"
}
plugins {
// plugins for the build system only
build ":tomcat:7.0.55.2" // or ":tomcat:8.0.20"
// plugins for the compile step
compile ":scaffolding:2.1.2"
compile ':cache:1.1.8'
compile ":asset-pipeline:2.1.5"
// plugins needed at runtime but not for compilation
runtime ":hibernate4:4.3.8.1" // or ":hibernate:3.6.10.18"
runtime ":database-migration:1.4.0"
runtime ":jquery:1.11.1"
// Uncomment these to enable additional asset-pipeline capabilities
//compile ":sass-asset-pipeline:1.9.0"
//compile ":less-asset-pipeline:1.10.0"
//compile ":coffee-asset-pipeline:1.8.0"
//compile ":handlebars-asset-pipeline:1.3.0.3"
}
}
Validate that you have the following configuration in your BuildConfig
dependencies {
...
compile 'org.grails.plugins:hibernate:4.3.10.4'
provided 'org.postgresql:postgresql:9.4-1203-jdbc4'
...
}

Connect to Oracle 12c Database from Grails

How do I configure Grails web app to connect to a simple (one table!) Oracle 12c database? I've been through a bunch of tutorials already, and each one is either incomplete or out of date. I need a simple, hand-holding, step-by-step tutorial. I understand that GORM is based on Hibernate, and somehow it's all taken care of under the hood, but I can't get a simple connection working. I've glanced over the Grails documentation but it seems to favour H2 and MySQL connections, not really Oracle.
So I understand that I have to modify DataSource.groovy, to replace the default H2 settings. Below is my attempt at modifying DataSource.groovy for my Oracle 12c database:
dataSource {
pooled = true
driverClassName = "oracle.jdbc.driver.OracleDriver"
username = "scott"
password = "Sc0ttSc0tt"
dialect = "org.hibernate.dialect.OracleDialect"
}
hibernate {
cache.use_second_level_cache = true
cache.use_query_cache = false
cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory' // Hibernate 3
temp.use_jdbc_metadata_defaults = false
}
// environment specific settings
environments {
development {
dataSource {
pooled - true
dialect = "org.hibernate.dialect.OracleDialect"
driverClassName = 'oracle.jdbc.OracleDriver'
username = 'scott'
password = 'Sc0ttSc0tt'
url = "jdbc:oracle:thin:#192.168.0.105:1521:orcl"
dbCreate = "validate" // one of 'create', 'create-drop', 'update', 'validate', ''
}
}
test {
dataSource {
pooled = true
dialect = "org.hibernate.dialect.OracleDialect"
driverClassName = 'oracle.jdbc.OracleDriver'
username = 'scott'
password = 'Sc0ttSc0tt'
url = 'jdbc:oracle:thin:#192.168.0.105:1521:orcl'
dbCreate = 'validate'
}
}
production {
dataSource {
pooled = true
dialect = "org.hibernate.dialect.OracleDialect"
driverClassName = 'oracle.jdbc.OracleDriver'
username = 'scott'
password = 'Sc0ttSc0tt'
url = 'jdbc:oracle:thin:#192.168.0.105:1521:orcl'
dbCreate = 'validate'
}
}
}
Then I understand that I can somehow use "Scaffolding" or GORM or whatever else to map domain classes to the database table... which is where I'm stuck, and am either not drinking enough coffee or have missed something.
Can anybody help please?
Thanks in advance.
You are using an older dialect, "org.hibernate.dialect.Oracle10gDialect" is the one you need. (At least for me connecting to a Oracle11gR2 DB)
The dialect you are using is for Oracle9g and before for my experience.
After playing around with the Datasource.groovy file, and making the changes recommended above by mwaisgold, I finally got the following format to work:
dataSource {
pooled = true
driverClassName = "oracle.jdbc.OracleDriver"
username = "scott"
password = "Sc0ttSc0tt"
}
hibernate {
cache.use_second_level_cache = true
cache.use_query_cache = false
cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory' // Hibernate 3
// cache.region.factory_class = 'org.hibernate.cache.ehcache.EhCacheRegionFactory' // Hibernate 4
temp.use_jdbc_metadata_defaults = false
}
// environment specific settings
environments {
development {
dataSource {
dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''
dialect = "org.hibernate.dialect.Oracle10gDialect"
url = "jdbc:oracle:thin:#192.168.0.103:1521:orcl"
}
}
test {
dataSource {
dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''
dialect = "org.hibernate.dialect.Oracle10gDialect"
url = "jdbc:oracle:thin:#192.168.0.103:1521:orcl"
}
}
production {
dataSource {
dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''
dialect = "org.hibernate.dialect.Oracle10gDialect"
url = "jdbc:oracle:thin:#192.168.0.103:1521:orcl"
}
}
}

Grails - Connection Closed message in Tomcat 7

I have a grails application that connects to an Oracle database over the firewall. I run into "Connection Closed" message.
I am not sure as to why it's happening. My production datasource is configured as follows:
production {
datasource {
dbCreate = "update"
url = ${datasource.url}
}
}
This isn't valid syntax: url = ${datasource.url}. It should be url = "${datasource.url}". Where is datasource.url coming from? If it's a string, you can just use url = datasource.url

Resources